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 autocorrect is not safe #12097

Closed
yenshirak opened this issue Aug 4, 2023 · 0 comments · Fixed by #12114
Closed

Style/ClassEqualityComparison autocorrect is not safe #12097

yenshirak opened this issue Aug 4, 2023 · 0 comments · Fixed by #12114
Labels

Comments

@yenshirak
Copy link

It is not safe to autocorrect foo.class.name == 'Bar' to foo.instance_of?(::Bar) because ::Bar may not be defined.


Expected behavior

The Style/ClassEqualityComparison cop autocorrect shouldn't be marked as safe because the autocorrected code may cause NameError.

Actual behavior

foo.class.name == 'Bar' is autocorrected to foo.instance_of?(::Bar).

Steps to reproduce the problem

Create a file with foo.class.name == 'Bar' and run rubocop -a.

RuboCop version

1.55.1

@koic koic added the bug label Aug 10, 2023
koic added a commit to koic/rubocop that referenced this issue Aug 10, 2023
…omparison`

Fixes rubocop#12097.

This PR marks unsafe autocorrect for `Style/ClassEqualityComparison`
because there is no guarantee that the constant `Foo` exists when
autocorrecting `var.class.name == 'Foo'` to `var.instance_of?(Foo)`.
bbatsov pushed a commit that referenced this issue Sep 28, 2023
Fixes #12097.

This PR marks unsafe autocorrect for `Style/ClassEqualityComparison`
because there is no guarantee that the constant `Foo` exists when
autocorrecting `var.class.name == 'Foo'` to `var.instance_of?(Foo)`.
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