Skip to content

Commit

Permalink
[Fix #7574] Fix corner case in Style/GuardClause
Browse files Browse the repository at this point in the history
  • Loading branch information
buehmann committed Dec 18, 2019
1 parent dff7052 commit b88ff75
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [#7513](https://github.com/rubocop-hq/rubocop/issues/7513): Fix abrupt error on autocorrecting with `--disable-uncorrectable`. ([@tejasbubane][])
* [#7537](https://github.com/rubocop-hq/rubocop/issues/7537): Fix a false positive for `Layout/SpaceAroundOperators` when using a Rational literal with `/` (e.g. `2/3r`). ([@koic][])
* [#7029](https://github.com/rubocop-hq/rubocop/issues/7029): Make `Style/Attr` not flag offense for custom `attr` method. ([@tejasbubane][])
* [#7574](https://github.com/rubocop-hq/rubocop/issues/7574): Fix a corner case that made `Style/GuardClause` crash. ([@buehmann][])

### Changes

Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/style/guard_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ def on_def(node)

if body.if_type?
check_ending_if(body)
elsif body.begin_type? && body.children.last.if_type?
check_ending_if(body.children.last)
elsif body.begin_type?
final_expression = body.children.last
check_ending_if(final_expression) if final_expression&.if_type?
end
end
alias on_defs on_def
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/style/guard_clause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ def func
RUBY
end

it 'accepts a method with empty parentheses as its body' do
expect_no_offenses(<<~RUBY)
def func
()
end
RUBY
end

context 'MinBodyLength: 1' do
let(:cop_config) do
{ 'MinBodyLength' => 1 }
Expand Down

0 comments on commit b88ff75

Please sign in to comment.