From e432b72fdbd6ae22ea0ce6658365ae330091a291 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 15 Mar 2018 19:08:18 +0900 Subject: [PATCH] Fix false positive for `Style/EmptyLineAfterGuardClause` Related #5679. This PR fixes a false positive for Style/EmptyLineAfterGuardClause when guard clause is before `else`. ## Reproduction code ```ruby def foo if cond return another_object if something_different? else bar end end ``` ## Expected behavior No offenses. ## Actual behavior and Steps to reproduce the problem ```console % rubocop /tmp/example.rb --only Style/EmptyLineAfterGuardClause Inspecting 1 file C Offenses: /tmp/example.rb:3:5: C: Style/EmptyLineAfterGuardClause: Add empty line after guard clause. return another_object if something_different? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 file inspected, 1 offense detected ``` ## RuboCop version ```console % rubocop -V 0.53.0 (using Parser 2.5.0.4, running on ruby 2.5.0 x86_64-darwin17) ``` This PR fixes the above false positive. --- CHANGELOG.md | 1 + .../style/empty_line_after_guard_clause.rb | 10 +++++ .../empty_line_after_guard_clause_spec.rb | 38 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 248cb9d6701e..71c2fb759d96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ * [#5674](https://github.com/bbatsov/rubocop/issues/5674): Fix auto-correction of `Layout/EmptyComment` when the empty comment appears on the same line as code. ([@rrosenblum][]) * [#5679](https://github.com/bbatsov/rubocop/pull/5679): Fix a false positive for `Style/EmptyLineAfterGuardClause` when guard clause is before `rescue` or `ensure`. ([@koic][]) * [#5694](https://github.com/bbatsov/rubocop/issues/5694): Match Rails versions with multiple digits when reading the TargetRailsVersion from the bundler lock files. ([@roberts1000][]) +* [#5700](https://github.com/bbatsov/rubocop/pull/5700): Fix a false positive for `Style/EmptyLineAfterGuardClause` when guard clause is before `else`. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/style/empty_line_after_guard_clause.rb b/lib/rubocop/cop/style/empty_line_after_guard_clause.rb index 6b906526a3d8..61b689fd04e1 100644 --- a/lib/rubocop/cop/style/empty_line_after_guard_clause.rb +++ b/lib/rubocop/cop/style/empty_line_after_guard_clause.rb @@ -44,6 +44,7 @@ def on_if(node) return unless contains_guard_clause?(node) return if next_line_rescue_or_ensure?(node) + return if next_sibling_parent_empty_or_else?(node) return if next_sibling_empty_or_guard_clause?(node) return if next_line_empty?(node) @@ -73,6 +74,15 @@ def next_line_rescue_or_ensure?(node) parent.nil? || parent.rescue_type? || parent.ensure_type? end + def next_sibling_parent_empty_or_else?(node) + next_sibling = node.parent.children[node.sibling_index + 1] + return true if next_sibling.nil? + + parent = next_sibling.parent + + parent && parent.if_type? && parent.else? + end + def next_sibling_empty_or_guard_clause?(node) next_sibling = node.parent.children[node.sibling_index + 1] return true if next_sibling.nil? diff --git a/spec/rubocop/cop/style/empty_line_after_guard_clause_spec.rb b/spec/rubocop/cop/style/empty_line_after_guard_clause_spec.rb index 41b5854237f1..02369e4bc159 100644 --- a/spec/rubocop/cop/style/empty_line_after_guard_clause_spec.rb +++ b/spec/rubocop/cop/style/empty_line_after_guard_clause_spec.rb @@ -109,6 +109,44 @@ def foo RUBY end + it 'does not register offence when guard clause is before `rescue`-`else`' do + expect_no_offenses(<<-RUBY.strip_indent) + def foo + begin + bar + rescue SomeException + return another_object if something_different? + else + bar + end + end + RUBY + end + + it 'does not register offence when guard clause is before `else`' do + expect_no_offenses(<<-RUBY.strip_indent) + def foo + if cond + return another_object if something_different? + else + bar + end + end + RUBY + end + + it 'does not register offence when guard clause is before `elsif`' do + expect_no_offenses(<<-RUBY.strip_indent) + def foo + if cond + return another_object if something_different? + elsif + bar + end + end + RUBY + end + it 'registers an offence for methods starting with end_' do expect_offense(<<-RUBY.strip_indent) def foo