You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rubocop doesn't suggest using #instance_of? with an arguments whose type it doesn't know.
Rubocop doesn't automatically "correct" code to #instance_of? when it isn't sure the change is safe.
Actual behavior
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.
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:
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:
…lityComparison`
Fixesrubocop#11168.
This PR fixes an incorrect autocorrect for `Style/ClassEqualityComparison`
when using instance variable comparison in module.
Expected behavior
#instance_of?
with an arguments whose type it doesn't know.#instance_of?
when it isn't sure the change is safe.Actual behavior
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.object.instance_of?(@klass_name)
, resulting inTypeError
at runtime.Additionally, when the method is in a module/class like in the example below:
then Rubocop converts it to:
This produces a syntax error in Ruby 3.1.
Steps to reproduce the problem
test.rb
with the following code:rubocop test.rb
.RuboCop version
Include the output of
rubocop -V
orbundle 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:The text was updated successfully, but these errors were encountered: