I'm using RuboCop 0.46.0 and Ruby 2.3.1.
Style/FrozenStringLiteralComment:
EnforcedStyle: always
# frozen_string_literal: true
MY_CONSTANT = 'mystring'
When run, rubocop -D
returns this:
Inspecting 1 file
C
Offenses:
constant.rb:3:15: C: Style/MutableConstant: Freeze mutable objects assigned to constants.
MY_CONSTANT = 'mystring'
^^^^^^^^^^
1 file inspected, 1 offense detected
Is there a reason MutableConstant isn't observing my frozen string literal comment?
Adding an empty line below the string literal line fixed it for me.
Among the many changes you can expect from this version is the confusingly named “Frozen String Literal Pragma.” This feature lets you add a magic comment to your Ruby source code, which makes all string literals frozen by default—that is, you won't be able to modify them.
Not every check makes sense for all Ruby versions. Tell RuboCop the Ruby version to check against.
Add the following to your .rubocop.yml
:
AllCops:
TargetRubyVersion: 2.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With