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

[Fix #12668] Fix an incorrect autocorrect for Lint/EmptyConditionalBody #12670

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12668](https://github.com/rubocop/rubocop/issues/12668): Fix an incorrect autocorrect for `Lint/EmptyConditionalBody` when missing `if` body with conditional `else` body. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/empty_conditional_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def remove_empty_branch(corrector, node)
def correct_other_branches(corrector, node)
return unless require_other_branches_correction?(node)

if node.else_branch&.if_type?
if node.else_branch&.if_type? && !node.else_branch.modifier_form?
# Replace an orphaned `elsif` with `if`
corrector.replace(node.else_branch.loc.keyword, 'if')
else
Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/lint/empty_conditional_body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,38 @@ class Foo
RUBY
end

it 'registers an offense for missing `if` body with conditional `else` body' do
expect_offense(<<~RUBY)
if condition
^^^^^^^^^^^^ Avoid `if` branches without a body.
else
do_something if x
end
RUBY

expect_correction(<<~RUBY)
unless condition
do_something if x
end
RUBY
end

it 'registers an offense for missing `unless` body with conditional `else` body' do
expect_offense(<<~RUBY)
unless condition
^^^^^^^^^^^^^^^^ Avoid `unless` branches without a body.
else
do_something if x
end
RUBY

expect_correction(<<~RUBY)
if condition
do_something if x
end
RUBY
end

it 'registers an offense for missing `unless` and `else` body' do
expect_offense(<<~RUBY)
unless condition
Expand Down