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

AutoCorrect breaks class name comparison #11762

Closed
denisenkom opened this issue Apr 5, 2023 · 0 comments · Fixed by #11764
Closed

AutoCorrect breaks class name comparison #11762

denisenkom opened this issue Apr 5, 2023 · 0 comments · Fixed by #11764
Labels

Comments

@denisenkom
Copy link

RuboCop auto-correction feature makes invalid correction for class name comparison. For example it replaces obj.class.name == cls_name with obj.instance_of?(cls_name). This is not correct since instance_of() method expects a class as a parameter and not a name of the class. As a result after this replacement code fails with the following error:

Traceback (most recent call last):
	1: from test.rb:2:in `<main>'
test.rb:2:in `instance_of?': class or module required (TypeError)

Expected behavior

RuboCop should not perform such replacement, for example obj.class.name == cls_name should stay unmodified.

Actual behavior

RuboCop replaced obj.class.name == cls_name with invalid Ruby code: obj.instance_of?(cls_name)

Steps to reproduce the problem

Create test.rb file with the following content:

cls_name = 'String'
raise 'Failed' unless 'somestr'.class.name == cls_name
puts 'SUCCESS'

Verify that created file works by running ruby test.rb, it should output SUCCESS

Run RubuCop auto-correction: rubocop --auto-correct

Now if you run ruby test.rb it will fail with error:

Traceback (most recent call last):
	1: from test.rb:2:in `<main>'
test.rb:2:in `instance_of?': class or module required (TypeError)

RuboCop version

~/.runtimes/Ruby27x/lib/ruby/gems/2.7.0/gems/rubocop-1.49.0/exe/rubocop -V
1.49.0 (using Parser 3.2.2.0, rubocop-ast 1.28.0, running on ruby 2.7.3) [x86_64-darwin21]
@koic koic added the bug label Apr 5, 2023
koic added a commit to koic/rubocop that referenced this issue Apr 5, 2023
…lityComparison`

Fixes rubocop#11762.

This PR fixes an incorrect autocorrect for `Style/ClassEqualityComparison`
when comparing a variable or return value for equality.

when a variable or return value of a method is used, it cannot be suggested
and safe autocorrected because the type is not known.
So this PR tweaks the cop that doesn't suggest `instance_of` argument at those times
and doesn't autocorrect. Anyway, detection is possible.
bbatsov pushed a commit that referenced this issue Apr 5, 2023
…parison`

Fixes #11762.

This PR fixes an incorrect autocorrect for `Style/ClassEqualityComparison`
when comparing a variable or return value for equality.

when a variable or return value of a method is used, it cannot be suggested
and safe autocorrected because the type is not known.
So this PR tweaks the cop that doesn't suggest `instance_of` argument at those times
and doesn't autocorrect. Anyway, detection is possible.
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