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

Naming/ConstantName ignores constants that are frozen #5155

Closed
dominicsayers opened this issue Nov 28, 2017 · 1 comment · Fixed by #5157
Closed

Naming/ConstantName ignores constants that are frozen #5155

dominicsayers opened this issue Nov 28, 2017 · 1 comment · Fixed by #5157
Labels

Comments

@dominicsayers
Copy link
Contributor

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)
@dominicsayers dominicsayers changed the title Naming/ConstantName ignores mutable constants that are frozen Naming/ConstantName ignores constants that are frozen Nov 28, 2017
@pocke pocke added the bug label Nov 28, 2017
@pocke
Copy link
Collaborator

pocke commented Nov 28, 2017

The cause:

https://github.com/bbatsov/rubocop/blob/4a0d6361d0065ca16ba19bdf3b3d6c4623e14adc/lib/rubocop/cop/naming/constant_name.rb#L27-L31

koic added a commit to koic/rubocop that referenced this issue Nov 28, 2017
Fixes rubocop#5155.

This PR fixes a false negative for `Naming/ConstantName` cop when
using frozen object assignment.

It also fixed the offenses that occurred in RuboCop itself.
bbatsov pushed a commit that referenced this issue Dec 1, 2017
This PR fixes a false negative for `Naming/ConstantName` cop when
using frozen object assignment.

It also fixed the offenses that occurred in RuboCop itself.
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.

2 participants