Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Style/HashEachMethods when a block with both parameters has no body #12636

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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