Skip to content

Commit

Permalink
Fix false positive for Style/EmptyLineAfterGuardClause
Browse files Browse the repository at this point in the history
Related rubocop#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.
  • Loading branch information
koic committed Mar 19, 2018
1 parent f78a4e0 commit e432b72
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions lib/rubocop/cop/style/empty_line_after_guard_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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?
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/style/empty_line_after_guard_clause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e432b72

Please sign in to comment.