Skip to content

Commit

Permalink
Fix an infinite loop error for Style/EmptyMethod
Browse files Browse the repository at this point in the history
This PR fixes the following infinite loop error for `Style/EmptyMethod`.

```ruby
% cat example.rb
def foo(arg
    ); end
```

## Before

The following error occurs without auto-correction.

```console
% bundle exec rubocop --only Style/EmptyMethod -a example.rb
(snip)

Offenses:

example.rb:1:1: C: [Corrected] Style/EmptyMethod: Put empty method
definitions on a single line.
def foo(arg ...
^^^^^^^^^^^^

0 files inspected, 1 offense detected, 1 offense corrected
Infinite loop detected in
/Users/koic/src/github.com/koic/rubocop-issues/empty_method/example.rb.
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:288:in `check_for_infinite_loop'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:271:in `block in iterate_until_no_changes'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:270:in `loop'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:270:in `iterate_until_no_changes'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:241:in `do_inspection_loop'
(snip)

% cat example.rb
def foo(arg
    ); end
```

## After

Auto-corrects without any error.

```console
% bundle exec rubocop --only Style/EmptyMethod -a example.rb
(snip)

Offenses:

example.rb:1:1: C: [Corrected] Style/EmptyMethod: Put empty method
definitions on a single line.
def foo(arg ...
^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

% cat example.rb
def foo(arg); end
```
  • Loading branch information
koic committed Jul 20, 2020
1 parent c1faddf commit 9cf9ceb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@
* [#8299](https://github.com/rubocop-hq/rubocop/issues/8299): Fix an incorrect auto-correct for `Style/RedundantCondition` when using `raise`, `rescue`, or `and` without argument parentheses in `else`. ([@koic][])
* [#8335](https://github.com/rubocop-hq/rubocop/issues/8335): Fix incorrect character class detection for nested or POSIX bracket character classes in `Style/RedundantRegexpEscape`. ([@owst][])
* [#8347](https://github.com/rubocop-hq/rubocop/issues/8347): Fix an incorrect auto-correct for `EnforcedStyle: hash_rockets` of `Style/HashSyntax` with `Layout/HashAlignment`. ([@koic][])
* [#8375](https://github.com/rubocop-hq/rubocop/pull/8375): Fix an infinite loop error for `Style/EmptyMethod`. ([@koic][])

### Changes

Expand Down
10 changes: 5 additions & 5 deletions lib/rubocop/cop/style/empty_method.rb
Expand Up @@ -73,13 +73,13 @@ def correct_style?(node)
end

def corrected(node)
if node.arguments?
arguments = node.arguments.source
extra_space = ' ' unless parentheses?(node.arguments)
end
scope = node.receiver ? "#{node.receiver.source}." : ''
arguments = if node.arguments?
args = node.arguments.map(&:source).join(', ')

signature = [scope, node.method_name, extra_space, arguments].join
parentheses?(node.arguments) ? "(#{args})" : " #{args}"
end
signature = [scope, node.method_name, arguments].join

["def #{signature}", 'end'].join(joint(node))
end
Expand Down
5 changes: 5 additions & 0 deletions spec/rubocop/cop/style/empty_method_spec.rb
Expand Up @@ -61,6 +61,11 @@
'end'].join("\n"),
'def foo; end'

it_behaves_like 'code with offense',
['def foo(arg',
'); end'].join("\n"),
'def foo(arg); end'

it_behaves_like 'code without offense',
'def foo; end'
end
Expand Down

0 comments on commit 9cf9ceb

Please sign in to comment.