diff --git a/changelog/fix_an_error_for_style_hash_each_methods.md b/changelog/fix_an_error_for_style_hash_each_methods.md new file mode 100644 index 000000000000..fafdad161984 --- /dev/null +++ b/changelog/fix_an_error_for_style_hash_each_methods.md @@ -0,0 +1 @@ +* [#12636](https://github.com/rubocop/rubocop/pull/12636): Fix an error for `Style/HashEachMethods` when a block with both parameters has no body. ([@earlopain][]) diff --git a/lib/rubocop/cop/style/hash_each_methods.rb b/lib/rubocop/cop/style/hash_each_methods.rb index d8456ed54158..a667614c044c 100644 --- a/lib/rubocop/cop/style/hash_each_methods.rb +++ b/lib/rubocop/cop/style/hash_each_methods.rb @@ -71,6 +71,8 @@ def on_block(node) # rubocop:disable Metrics/AbcSize def check_unused_block_args(node, key, value) + return if node.body.nil? + value_unused = unused_block_arg_exist?(node, value) key_unused = unused_block_arg_exist?(node, key) return if value_unused && key_unused diff --git a/spec/rubocop/cop/style/hash_each_methods_spec.rb b/spec/rubocop/cop/style/hash_each_methods_spec.rb index 5296d4862ef3..66f9c302d125 100644 --- a/spec/rubocop/cop/style/hash_each_methods_spec.rb +++ b/spec/rubocop/cop/style/hash_each_methods_spec.rb @@ -109,6 +109,10 @@ expect_no_offenses('foo.each { |k, v| do_something }') end + it 'does not register an offense when the body of `Enumerable#each` is empty' do + expect_no_offenses('foo.each { |k, v| }') + end + it 'registers an offense when the rest value block argument of `Enumerable#each` method is unused' do expect_offense(<<~RUBY) foo.each { |k, *v| do_something(*v) }