From 1fe7d0d4e80ff4d20bdb3e227a3ba4713578feba Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 9 Apr 2022 16:07:50 +0900 Subject: [PATCH] [Fix #10514] Fix an error for `Lint/EmptyConditionalBody` Fixes #10514. This PR fixes an error for `Lint/EmptyConditionalBody` when missing second `elsif` body. --- .../fix_an_error_for_lint_empty_conditional_body.md | 1 + lib/rubocop/cop/mixin/comments_help.rb | 4 +++- spec/rubocop/cop/lint/empty_conditional_body_spec.rb | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_an_error_for_lint_empty_conditional_body.md diff --git a/changelog/fix_an_error_for_lint_empty_conditional_body.md b/changelog/fix_an_error_for_lint_empty_conditional_body.md new file mode 100644 index 000000000000..0180d046ae38 --- /dev/null +++ b/changelog/fix_an_error_for_lint_empty_conditional_body.md @@ -0,0 +1 @@ +* [#10514](https://github.com/rubocop/rubocop/issues/10514): Fix an error for `Lint/EmptyConditionalBody` when missing second `elsif` body. ([@koic][]) diff --git a/lib/rubocop/cop/mixin/comments_help.rb b/lib/rubocop/cop/mixin/comments_help.rb index aad97f11fd00..102cb25fd8ea 100644 --- a/lib/rubocop/cop/mixin/comments_help.rb +++ b/lib/rubocop/cop/mixin/comments_help.rb @@ -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 diff --git a/spec/rubocop/cop/lint/empty_conditional_body_spec.rb b/spec/rubocop/cop/lint/empty_conditional_body_spec.rb index a49752a24c57..61fcfec5b23c 100644 --- a/spec/rubocop/cop/lint/empty_conditional_body_spec.rb +++ b/spec/rubocop/cop/lint/empty_conditional_body_spec.rb @@ -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