Skip to content

Commit

Permalink
Fix an error for Style/HashEachMethods when a block with both param…
Browse files Browse the repository at this point in the history
…eters has no body
  • Loading branch information
Earlopain committed Jan 20, 2024
1 parent 54c3f61 commit 17f5fcf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions 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][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/hash_each_methods.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/style/hash_each_methods_spec.rb
Expand Up @@ -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) }
Expand Down

0 comments on commit 17f5fcf

Please sign in to comment.