Skip to content

Commit

Permalink
Merge pull request rubocop#13659 from viralpraxis/fix-style-missing-e…
Browse files Browse the repository at this point in the history
…lse-cop-error-on-configured-empty-else-cop-multiple-elsifs

Fix `Style/MissingElse` cop error if `Style/EmptyElse`'s style is not `both` and if expression contains `elsif`
  • Loading branch information
koic authored Jan 9, 2025
2 parents 0b761c6 + 84bd73f commit 0b4e1b4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13659](https://github.com/rubocop/rubocop/pull/13659): Fix `Style/MissingElse` cop error if `Style/EmptyElse`'s `EnforcedStyle` is not `both` and `if` expression contains multiple `elsif`. ([@viralpraxis][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/missing_else.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def message_template
end

def autocorrect(corrector, node)
node = node.parent unless node.loc.end
node = node.ancestors.find { |ancestor| ancestor.loc.end } unless node.loc.end

case empty_else_style
when :empty
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/style/missing_else_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@
if cond_1; 1; elsif cond_2; 3; else; nil; end
RUBY
end

it 'registers an offense with multiple `elsif` clauses' do
expect_offense(<<~RUBY)
if cond_1; 1; elsif cond_2; 2; elsif cond_3; 3; end
^^^^^^^^^^^^^^^ `if` condition requires an `else`-clause with `nil` in it.
RUBY

expect_correction(<<~RUBY)
if cond_1; 1; elsif cond_2; 2; elsif cond_3; 3; else; nil; end
RUBY
end
end
end

Expand Down Expand Up @@ -404,6 +415,17 @@
if cond_1; 1; elsif cond_2; 3; else; end
RUBY
end

it 'registers an offense with multiple `elsif` clauses' do
expect_offense(<<~RUBY)
if cond_1; 1; elsif cond_2; 2; elsif cond_3; 3; end
^^^^^^^^^^^^^^^ `if` condition requires an empty `else`-clause.
RUBY

expect_correction(<<~RUBY)
if cond_1; 1; elsif cond_2; 2; elsif cond_3; 3; else; end
RUBY
end
end
end

Expand Down Expand Up @@ -533,6 +555,17 @@
if cond_1; 1; elsif cond_2; 3; else; end
RUBY
end

it 'registers an offense with multiple `elsif` clauses' do
expect_offense(<<~RUBY)
if cond_1; 1; elsif cond_2; 2; elsif cond_3; 3; end
^^^^^^^^^^^^^^^ `if` condition requires an empty `else`-clause.
RUBY

expect_correction(<<~RUBY)
if cond_1; 1; elsif cond_2; 2; elsif cond_3; 3; else; end
RUBY
end
end
end

Expand Down

0 comments on commit 0b4e1b4

Please sign in to comment.