Rubocop: Naming/ConstantName ignores constants that are frozen

Created on 28 Nov 2017  路  1Comment  路  Source: rubocop-hq/rubocop

If you give a constant a name that is not in SCREAMING_SNAKE_CASE the cop _Naming/ConstantName_ raises an offence. If you then .freeze that constant the offence is not detected.


Expected behavior

I expect Rubocop's _Naming/ConstantName_ cop to raise an offence for a mis-named constant whether the constant is frozen or not.

Actual behavior

If the mis-named constant is frozen with .freeze the _Naming/ConstantName_ offence is not detected.

Steps to reproduce the problem

  1. Create a class in my_class.rb as follows:
class MyClass
  MyConstant = "x"
end
  1. Run Rubocop against that class
$ rubocop my_class.rb
Inspecting 1 file
C

Offenses:

tmp/my_class.rb:2:3: C: Naming/ConstantName: Use SCREAMING_SNAKE_CASE for constants. (https://github.com/bbatsov/ruby-style-guide#screaming-snake-case)
  MyConstant = "x"
  ^^^^^^^^^^
tmp/my_class.rb:2:16: C: Style/MutableConstant: Freeze mutable objects assigned to constants.
  MyConstant = "x"
               ^^^
  1. Amend the class so that the constant is frozen
class MyClass
  MyConstant = "x".freeze
end
  1. Run Rubocop against that class again
$ rubocop my_class.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

RuboCop version

Include the output of rubocop -V. Here's an example:

$ rubocop -V
0.51.0 (using Parser 2.4.0.2, running on ruby 2.2.2 x86_64-darwin16)
bug

>All comments

Was this page helpful?
0 / 5 - 0 ratings