Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/MutableConstant false positive for heredoc with frozen_string_literal #10070

Closed
denys-husiev-perfectial opened this issue Sep 8, 2021 · 4 comments · Fixed by #10071
Closed
Labels

Comments

@denys-husiev-perfectial
Copy link

denys-husiev-perfectial commented Sep 8, 2021

Rubocop asks to freeze constant string(written with multiline heredoc style) when # frozen_string_literal: true


Expected behavior

No error occures

Actual behavior

[Correctable] Style/MutableConstant: Freeze mutable objects assigned to constants

Steps to reproduce the problem

# frozen_string_literal: true

class MyClass

  MY_CONSTANT = <<~LUA # raises Style/MutableConstant offence
    lorem ipsum
    lorem ipsum
  LUA

end

RuboCop version

rubocop -V
1.20.0 (using Parser 3.0.2.0, rubocop-ast 1.11.0, running on ruby 3.0.2 x86_64-darwin20)
  - rubocop-faker 1.1.0
  - rubocop-performance 1.11.5
  - rubocop-rails 2.11.3
  - rubocop-rspec 2.4.0

.rubocop.yml
  TargetRubyVersion: 3.0
@koic koic added the bug label Sep 8, 2021
koic added a commit to koic/rubocop that referenced this issue Sep 8, 2021
Fixes rubocop#10070.

This PR fixes a false positive for `Style/MutableConstant`
when using non-interpolated heredoc in Ruby 3.0.
bbatsov pushed a commit that referenced this issue Sep 9, 2021
Fixes #10070.

This PR fixes a false positive for `Style/MutableConstant`
when using non-interpolated heredoc in Ruby 3.0.
@Zhao-Andy
Copy link

Zhao-Andy commented Oct 1, 2021

Hello, I was working on a related example and I was wondering, this should be frozen after all right? I ran the Rubocop auto correcter and it corrected to this:

# frozen_string_literal: true

class MyClass

  MY_CONSTANT = <<~LUA.freeze # calling .freeze does not raise Style/MutableConstant offense
    lorem ipsum
    lorem ipsum
  LUA

end

Feel free to correct me if I'm wrong as I don't work with frozen HEREDOCs too much. :)

RuboCop version

rubocop -V
1.22.0 (using Parser 3.0.2.0, rubocop-ast 1.12.0, running on ruby 3.0.2 x86_64-linux)
  - rubocop-performance 1.11.5
  - rubocop-rails 2.12.2
  - rubocop-rspec 2.5.0
  
.rubocop.yml
  TargetRubyVersion: 3.0

@dvandersluis
Copy link
Member

Your heredoc would be frozen, and that .freeze triggers an Style/RedundantFreeze offense:

test.rb:5:17: C: [Correctable] Style/RedundantFreeze: Do not freeze immutable objects, as freezing them has no effect.
  MY_CONSTANT = <<~LUA.freeze # calling .freeze does not raise Style/MutableConstant offense
                ^^^^^^^^^^^^^

What was the original code that was autocorrected to your example?

@Zhao-Andy
Copy link

Original code was:

class MyClass

  MY_CONSTANT = <<~LUA
    lorem ipsum
    lorem ipsum
  LUA

end

Actually figured it out -- I think I had some cached version of Rubocop that was running (1.20.0).

Sorry for the noise, and thanks for the explanation!

@dvandersluis
Copy link
Member

Glad you figured it out @Zhao-Andy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants