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

Bad autocorrect for Style/CaseLikeIf #8508

Closed
jcoyne opened this issue Aug 10, 2020 · 5 comments · Fixed by #8511
Closed

Bad autocorrect for Style/CaseLikeIf #8508

jcoyne opened this issue Aug 10, 2020 · 5 comments · Fixed by #8511

Comments

@jcoyne
Copy link

jcoyne commented Aug 10, 2020

In rubocop 0.89.0

If you have code like this:

if klass == ClassA
  do_something
elsif klass == ClassB
  do_something_else
end

It incorrectly gets autocorrected to:

 case klass
 when ClassA
   do_something
 when ClassB
   do_something_else
 end

@marcandre warned about this here #7736 (comment) but I think this advice went unheeded.

@marcandre
Copy link
Contributor

This cop wasn't marked as Safe either 🤷‍♂️

@fatkodima would you like to handle this?

@fatkodima
Copy link
Contributor

fatkodima commented Aug 10, 2020

Can't reproduce.

$ cat test.rb
if klass == ClassA
  do_something
elsif klass == ClassB
  do_something_else
end

$ cat .rubocop.yml
...
Style/CaseLikeIf:
  Enabled: true
....

$ rubocop -v
0.89.0

$ rubocop test.rb -a
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
if klass == ClassA
^
test.rb:3:1: C: Style/MissingElse: if condition requires an else-clause.
elsif klass == ClassB ...
^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

@jcoyne
Copy link
Author

jcoyne commented Aug 10, 2020

Here is the "real" code where autocorrect went wrong for me: sul-dlss/dor-services-app@1a289b1#diff-c03d1fa455324b14d721e59ca497ab9eL27-L35

@jcoyne
Copy link
Author

jcoyne commented Aug 10, 2020

So it appears that it might have something to do with when it detects things are a class or a constant.

For example, this will trigger it:

if klass == Nokogiri::XML
  do_something
elsif klass == ClassB
  do_something_else
end
test.rb:3:1: C: Style/CaseLikeIf: Convert if-elsif to case-when.
if klass == Nokogiri::XML ...
^^^^^^^^^^^^^^^^^^^^^^^^^

This does not trigger it:

if klass == Nokogiri::Xml
  do_something
elsif klass == ClassB
  do_something_else
end

and it doesn't seem to care that things in elsif clauses are class-like.

@fatkodima
Copy link
Contributor

Thanks for the report. Should be fixed by #8511

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants