Skip to content

Commit

Permalink
[Fix rubocop#12761] Fix a false positive for Style/HashEachMethods
Browse files Browse the repository at this point in the history
Fixes rubocop#12761.

This PR fixes a false positive for `Style/HashEachMethods`
when the key block argument of `Enumerable#each` method is unused after `chunk`.
  • Loading branch information
koic committed Mar 8, 2024
1 parent 6d39e70 commit 85ac706
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12761](https://github.com/rubocop/rubocop/issues/12761): Fix a false positive for `Style/HashEachMethods` when the key block argument of `Enumerable#each` method is unused after `chunk`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/hash_each_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class HashEachMethods < Base

MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
UNUSED_BLOCK_ARG_MSG = "#{MSG.chop} and remove the unused `%<unused_code>s` block argument."
ARRAY_CONVERTER_METHODS = %i[assoc flatten rassoc sort sort_by to_a].freeze
ARRAY_CONVERTER_METHODS = %i[assoc chunk flatten rassoc sort sort_by to_a].freeze

# @!method kv_each(node)
def_node_matcher :kv_each, <<~PATTERN
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/hash_each_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@
RUBY
end

it 'does not register an offense when the key block argument of `Enumerable#each` method is unused after `chunk`' do
expect_no_offenses(<<~RUBY)
foo.chunk { |i| i.do_something }.each { |unused_key, v| do_something(v) }
RUBY
end

it 'does not register an offense when the key block argument of `Enumerable#each` method is unused after `flatten`' do
expect_no_offenses(<<~RUBY)
foo.flatten.each { |unused_key, v| do_something(v) }
Expand Down

0 comments on commit 85ac706

Please sign in to comment.