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/EmptyCaseCondition wrongly autocorrects multiple values #4283

Closed
maximeg opened this issue Apr 16, 2017 · 1 comment
Closed

Style/EmptyCaseCondition wrongly autocorrects multiple values #4283

maximeg opened this issue Apr 16, 2017 · 1 comment
Labels

Comments

@maximeg
Copy link

maximeg commented Apr 16, 2017

This is an issue about the Style/EmptyCaseCondition cop that wrongly autocorrects lines with multiples values.


Given a file with :

case
when my.foo?, my.bar?
  something
when my.baz?
  something_else
end

Expected behavior

File should be modified to:

if my.foo? || my.bar?
  something
elsif my.baz?
  something_else
end

Actual behavior

File is modified to:

if my.foo?, my.bar?
  something
elsif my.baz?
  something_else
end

Rubocop output is :

rubocop -D -a                
Inspecting 1 file
E

Offenses:

test.rb:1:1: C: [Corrected] Style/EmptyCaseCondition: Do not use empty case condition, instead use an if expression.
case
^^^^
test.rb:2:11: E: unexpected token tCOMMA
(Using Ruby 2.3 parser; configure using TargetRubyVersion parameter, under AllCops)
if my.foo?, my.bar?
          ^
test.rb:4:1: E: unexpected token kELSIF
(Using Ruby 2.3 parser; configure using TargetRubyVersion parameter, under AllCops)
elsif my.baz?
^^^^^
test.rb:6:1: E: unexpected token kEND
(Using Ruby 2.3 parser; configure using TargetRubyVersion parameter, under AllCops)
end
^^^

RuboCop version

$ rubocop -V
0.48.1 (using Parser 2.4.0.0, running on ruby 2.3.3 x86_64-darwin16)
@maximeg maximeg changed the title Style/EmptyCaseCondition wrongly autocorrect multiple values Style/EmptyCaseCondition wrongly autocorrects multiple values Apr 16, 2017
@bbatsov bbatsov added the bug label Apr 16, 2017
@robbie-smith
Copy link

I was able to reproduce the bug reported as well. I reproduced it using the same syntax and got the same results as above.

rubicon -D -a
Inspecting 1 file
E

Offenses:

test.rb:1:1: C: [Corrected] Style/EmptyCaseCondition: Do not use empty case condition, instead use an if expression.
case
^^^^
test.rb:1:5: C: [Corrected] Style/TrailingWhitespace: Trailing whitespace detected.
case
    ^
test.rb:1:11: E: unexpected token tCOMMA
(Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
if my.foo?, my.bar?
          ^
test.rb:3:1: E: unexpected token kELSIF
(Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
elsif my.baz?
^^^^^
test.rb:5:1: E: unexpected token kEND
(Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
end
^^^

1 file inspected, 5 offenses detected, 2 offenses corrected
rubocop -v
0.48.1

ilansh pushed a commit to ilansh/rubocop that referenced this issue Apr 22, 2017
When first `when` condition of an empty case included comma-delimited
alternatives, autocorrect did not replace the comma with `||`.
This commit fixes the issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants