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