Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rubocop MutableConstant not observing frozen string literal comment

Tags:

I'm using RuboCop 0.46.0 and Ruby 2.3.1.

.rubocop.yml

Style/FrozenStringLiteralComment:
  EnforcedStyle: always

constant.rb

# 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?

like image 486
Inityx Avatar asked Mar 28 '17 23:03

Inityx


People also ask

How do I fix missing frozen string literal comment?

Adding an empty line below the string literal line fixed it for me.

What is a frozen string literal?

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.


1 Answers

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
like image 150
spickermann Avatar answered Sep 21 '22 10:09

spickermann