Skip to content

Commit

Permalink
Merge pull request #12636 from Earlopain/fix-error-for-style-hash-eac…
Browse files Browse the repository at this point in the history
…h-methods

Fix an error for `Style/HashEachMethods` when a block with both parameters has no body
  • Loading branch information
koic authored Jan 21, 2024
2 parents 54c3f61 + 17f5fcf commit c3921c0
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 c3921c0

Please sign in to comment.