From 9cf9ceb1d48aae5149b4b7b63918c983aa694aca Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 20 Jul 2020 19:06:44 +0900 Subject: [PATCH] Fix an infinite loop error for `Style/EmptyMethod` 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 ``` --- CHANGELOG.md | 1 + lib/rubocop/cop/style/empty_method.rb | 10 +++++----- spec/rubocop/cop/style/empty_method_spec.rb | 5 +++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 029d08fbde9..efede0b2cb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/style/empty_method.rb b/lib/rubocop/cop/style/empty_method.rb index 07b47350e12..2dfe32c3bce 100644 --- a/lib/rubocop/cop/style/empty_method.rb +++ b/lib/rubocop/cop/style/empty_method.rb @@ -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 diff --git a/spec/rubocop/cop/style/empty_method_spec.rb b/spec/rubocop/cop/style/empty_method_spec.rb index 3d350c7abbc..e76094fed46 100644 --- a/spec/rubocop/cop/style/empty_method_spec.rb +++ b/spec/rubocop/cop/style/empty_method_spec.rb @@ -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