Skip to content

Commit

Permalink
Fix a false negative for Lint/ElseLayout
Browse files Browse the repository at this point in the history
Regression by 219dfe1.

This PR fixes a false negative for `Lint/ElseLayout`
when using multiple `elsif`s.
  • Loading branch information
koic authored and bbatsov committed Feb 12, 2021
1 parent b82c20f commit b2aa853
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_false_negative_for_lint_else_layout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9511](https://github.com/rubocop-hq/rubocop/pull/9511): Fix a false negative for `Lint/ElseLayout` when using multiple `elsif`s. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/else_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ElseLayout < Base
MSG = 'Odd `else` layout detected. Did you mean to use `elsif`?'

def on_if(node)
return if node.ternary? || node.elsif?
return if node.ternary?

check(node)
end
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/lint/else_layout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@
RUBY
end

it 'registers and corrects an offense when using multiple `elsif`s' do
expect_offense(<<~RUBY)
if condition_foo
foo
elsif condition_bar
bar
elsif condition_baz
baz
else qux
^^^ Odd `else` layout detected. Did you mean to use `elsif`?
quux
corge
end
RUBY

expect_correction(<<~RUBY)
if condition_foo
foo
elsif condition_bar
bar
elsif condition_baz
baz
else
qux
quux
corge
end
RUBY
end

it 'handles ternary ops' do
expect_no_offenses('x ? a : b')
end
Expand Down

0 comments on commit b2aa853

Please sign in to comment.