Rubocop: String literal should be freezed object with frozen_string_literal for constants

Created on 6 Mar 2017  Â·  7Comments  Â·  Source: rubocop-hq/rubocop

Rubocop is missing this case. Please, support this case.

Expected behavior

# test_rubocop.rb
# frozen_string_literal: true

VERSION = '0.0.0'
$ rubocop test_rubocop.rb 
Inspecting 1 file
.

1 file inspected, no offenses detected

Actual behavior

# 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

Steps to reproduce the problem

$ 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 version

$ rubocop -V
0.47.1 (using Parser 2.4.0.0, running on ruby 2.3.1 x86_64-darwin15)
bug

All 7 comments

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

TL;DR

  • FrozenStringLiteral is working well
  • Rubocop cannot detect correct ruby version

For me

I 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bbugh picture bbugh  Â·  3Comments

david942j picture david942j  Â·  3Comments

NobodysNightmare picture NobodysNightmare  Â·  3Comments

deivid-rodriguez picture deivid-rodriguez  Â·  3Comments

herwinw picture herwinw  Â·  3Comments