Skip to content

Commit

Permalink
[Fix rubocop#10514] Fix an error for Lint/EmptyConditionalBody
Browse files Browse the repository at this point in the history
Fixes rubocop#10514.

This PR fixes an error for `Lint/EmptyConditionalBody`
when missing second `elsif` body.
  • Loading branch information
koic committed Apr 9, 2022
1 parent 2d3a972 commit 0dc992e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_lint_empty_conditional_body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10514](https://github.com/rubocop/rubocop/issues/10514): Fix an error for `Lint/EmptyConditionalBody` when missing second `elsif` body. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/mixin/comments_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ def buffer
# Returns the end line of a node, which might be a comment and not part of the AST
# End line is considered either the line at which another node starts, or
# the line at which the parent node ends.
# rubocop:disable Metrics/AbcSize
def find_end_line(node)
if node.if_type? && node.loc.else
node.loc.else.line
elsif (next_sibling = node.right_sibling)
next_sibling.loc.line
elsif (parent = node.parent)
parent.loc.end.line
parent.loc.end ? parent.loc.end.line : parent.loc.line
else
node.loc.end.line
end
end
# rubocop:enable Metrics/AbcSize
end
end
end
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/empty_conditional_body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@
RUBY
end

it 'registers an offense for missing second `elsif` body without an inline comment' do
expect_offense(<<~RUBY)
if foo
do_foo
elsif bar
do_bar
elsif baz
^^^^^^^^^ Avoid `elsif` branches without a body.
end
RUBY
end

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

0 comments on commit 0dc992e

Please sign in to comment.