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/Next autocorrect breaks with nested conditionals #3788

Closed
Drenmi opened this issue Dec 15, 2016 · 0 comments
Closed

Style/Next autocorrect breaks with nested conditionals #3788

Drenmi opened this issue Dec 15, 2016 · 0 comments

Comments

@Drenmi
Copy link
Collaborator

Drenmi commented Dec 15, 2016

When autocorrecting a Style/Next offense with nested conditionals, it results in invalid code. It seems like the inner conditional is erroneously targeted for the correction. See example below.


Expected behavior

RuboCop autocorrects to:

foos.each do |foo|
  next if foo[:bar]

  if baz
    do_something
  else
    do_something_else
  end
end

Actual behavior

RuboCop autcorrects to:

foos.each do |foo|
  unless foo[:bar]
    next unless baz
      do_something
    else
      do_something_else
  end
end

(Note the missing end.)

Steps to reproduce the problem

Run bundle exec rubocop -a on:

foos.each do |foo|
  unless foo[:bar]
    if baz
      do_something
    else
      do_something_else
    end
  end
end

RuboCop version

$ rubocop -V
0.46.0 (using Parser 2.3.3.1, running on ruby 2.3.3 x86_64-darwin15)
Drenmi added a commit to Drenmi/rubocop that referenced this issue Dec 16, 2016
…d conditionals

When inspecting and auto-correcting code with nested conditionals where
the inner conditional has an `else` statement, but the outer conditional
does not, the auto-correct messes up:

```
foos.each do |foo|
  unless foo[:bar]
    if baz
      do_something
    else
      do_something_else
    end
  end
end
```

was corrected to:

```
foos.each do |foo|
  unless foo[bar]
    next unless baz
      do_something
    else
      do_something_else
    end
end
```

This change fixes that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant