Rubocop is missing this case. Please, support this case.
# test_rubocop.rb
# frozen_string_literal: true
VERSION = '0.0.0'
$ rubocop test_rubocop.rb
Inspecting 1 file
.
1 file inspected, no offenses detected
# test_rubocop.rb
# frozen_string_literal: true
VERSION = '0.0.0'
$ rubocop test_rubocop.rb
Inspecting 1 file
C
Offenses:
test_rubocop.rb:4:11: C: Freeze mutable objects assigned to constants.
VERSION = '0.0.0'
^^^^^^^
1 file inspected, 1 offense detected
$ gem install rubocop
$ editor test_rubocop.rb
# test_rubocop.rb
# frozen_string_literal: true
VERSION = '0.0.0'
$ rubocop test_rubocop.rb
...
And such case is correct:
# test_rubocop.rb
VERSION = '0.0.0'.freeze
$ rubocop test_rubocop.rb
Inspecting 1 file
.
1 file inspected, no offenses detected
$ rubocop -V
0.47.1 (using Parser 2.4.0.0, running on ruby 2.3.1 x86_64-darwin15)
According to the code here rubocop checks for first 3 lines of the source code for a frozen_string magic comment.
I could not reproduce your issue:
⇒ cat frozen_string.rb
# test_rubocop.rb
# frozen_string_literal: true
VERSION = '0.0.0'
⇒ rubocop frozen_string.rb
Inspecting 1 file
.
1 file inspected, no offenses detected
⇒ rubocop -V
0.47.1 (using Parser 2.3.3.1, running on ruby 2.3.1 x86_64-darwin15)
⇒ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
Is there something specific in your rubocop config @mizunashi-mana ?
@tejasbubane Really...? I use default config(not specify and without local config). I will retry and send more feedback at a later date.
I use rbenv for global ruby (use rbenv global 2.3.3).
Rubocop detects ruby version by config or local rbenv specified:
https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/config.rb#L294
Because I provided no hints for rubocop, rubocop used default version(2.1).
frozen_string_literal comment is supported >=2.3. So, MutableConstant is not working by this line:
https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/mixin/frozen_string_literal.rb#L23
FrozenStringLiteral is working wellI specify TargetRubyVersion to fix this issue. This fix works good.
I don't know rubocop's roadmap and policy clearly. If this acts is correct for rubocop community, I will close this issue.
What should I do? @bbatsov
I have been trying, but I cannot reproduce the issue.
rrosenblum@C02NV0KSG3QN:~/work/test$ cat .ruby-version
2.3.3
rrosenblum@C02NV0KSG3QN:~/work/test$ cat test.rb
# test_rubocop.rb
# frozen_string_literal: true
VERSION = '0.0.0'
rrosenblum@C02NV0KSG3QN:~/work/test$ bundle exec rubocop test.rb
Inspecting 1 file
.
1 file inspected, no offenses detected
rrosenblum@C02NV0KSG3QN:~/work/test$ bundle exec rubocop -V
0.47.1 (using Parser 2.4.0.0, running on ruby 2.3.3 x86_64-darwin14)
rrosenblum@C02NV0KSG3QN:~/work/test$ ruby --version
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin14]
Reproducing is easy. Try it:
rm .ruby-version
bundle exec rubocop test.rb
But, this stands my less understanding. (ref: https://github.com/bbatsov/rubocop/blob/master/config/default.yml#L79) I close this issue.
Bizarrely, I've only been able to reproduce this on a CI machine (Jenkins with the rbenv plugin). But I can't figure out where it differs from my local environment.
Aha, as @mizunashi-mana suggested it was the .ruby-version file. That didn't exist on Jenkins, so it was falling back to the 2.0 parser. Adding TargetRubyVersion: 2.3 fixes things for me.