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/ClassEqualityComparison is overly aggressive #11168

Closed
gregnavis opened this issue Nov 9, 2022 · 0 comments · Fixed by #11170
Closed

Style/ClassEqualityComparison is overly aggressive #11168

gregnavis opened this issue Nov 9, 2022 · 0 comments · Fixed by #11170
Labels

Comments

@gregnavis
Copy link

Expected behavior

  1. Rubocop doesn't suggest using #instance_of? with an arguments whose type it doesn't know.
  2. Rubocop doesn't automatically "correct" code to #instance_of? when it isn't sure the change is safe.

Actual behavior

  1. Rubocop reported the following error:
test.rb:2:10: C: [Correctable] Style/ClassEqualityComparison: Use instance_of?(@klass_name) instead of comparing classes.
  object.class.name == @klass_name

The error message contains a suggestion to use #instance_of?(@klass_name), but Rubocop doesn't know the type of @klass_name. That suggestion can be incorrect in many cases.

  1. Rubocop changed the code above to object.instance_of?(@klass_name), resulting in TypeError at runtime.

Additionally, when the method is in a module/class like in the example below:

class MyClass
  def check_class?(object)
    object.class.name == @klass_name
  end
end

then Rubocop converts it to:

class MyClass
  def check_class?(object)
    object.instance_of?(::@klass_name)
  end
end

This produces a syntax error in Ruby 3.1.

Steps to reproduce the problem

  1. Create a file called test.rb with the following code:
class MyClass
  def check_class?(object)
    object.class.name == @klass_name
  end
end
  1. Call rubocop test.rb.

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

% bundle3.1 exec rubocop -V
1.38.0 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.2) [arm64-darwin22]
  - rubocop-rails 2.17.2
@koic koic added the bug label Nov 9, 2022
koic added a commit to koic/rubocop that referenced this issue Nov 9, 2022
…lityComparison`

Fixes rubocop#11168.

This PR fixes an incorrect autocorrect for `Style/ClassEqualityComparison`
when using instance variable comparison in module.
bbatsov pushed a commit that referenced this issue Nov 10, 2022
…parison`

Fixes #11168.

This PR fixes an incorrect autocorrect for `Style/ClassEqualityComparison`
when using instance variable comparison in module.
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