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 #12444] Fix false positive for Style/HashEachMethods #12508

Merged

Conversation

koic
Copy link
Member

@koic koic commented Dec 4, 2023

Fixes #12444.

This PR fixes false positive for Style/HashEachMethods when receiver literal is not a hash literal.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic koic force-pushed the fix_a_false_positive_for_style_hash_each_methods branch from 0927724 to aa1d624 Compare December 4, 2023 15:26

it "does not register an offense when `['array', 'literal'].keys.each`" do
expect_no_offenses(<<~RUBY)
['array', 'literal'].keys.each { |k| p k }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is broken.

2.7.6 :002 > ['array', 'literal'].keys.each { |k| p k }
Traceback (most recent call last):
        4: from /home/ydakuka/.rvm/rubies/ruby-2.7.6/bin/irb:23:in `<main>'
        3: from /home/ydakuka/.rvm/rubies/ruby-2.7.6/bin/irb:23:in `load'
        2: from /home/ydakuka/.rvm/rubies/ruby-2.7.6/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
        1: from (irb):1
NoMethodError (undefined method `keys' for ["array", "literal"]:Array)

Copy link

@ydakuka ydakuka Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue describes another case: there is a #each with two args and second one is not used.

[[1, 2, 3], [4, 5, 6]].each { |a, _| puts(a) }

And if I run rubocop, I'll get the following:

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop app/models/test.rb 
Inspecting 1 file
C

Offenses:

app/models/test.rb:30:5: C: [Correctable] Style/HashEachMethods: Use each_key instead of each and remove the unused _ block argument.
    [[1, 2, 3], [4, 5, 6]].each { |a, _| puts(a) }
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've fixed it.

@koic koic force-pushed the fix_a_false_positive_for_style_hash_each_methods branch 3 times, most recently from f5ad65b to 95d0e46 Compare December 8, 2023 08:01
@@ -88,6 +90,12 @@ def on_block_pass(node)

private

def handlable?(node)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you meant to write "handleable"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, this is a typo. I've fixed it.

Fixes rubocop#12444.

This PR fixes false positive for `Style/HashEachMethods`
when receiver literal is not a hash literal.
@koic koic force-pushed the fix_a_false_positive_for_style_hash_each_methods branch from 95d0e46 to 23a9f6f Compare December 10, 2023 03:34
@bbatsov bbatsov merged commit b1bcb31 into rubocop:master Dec 10, 2023
28 checks passed
@koic koic deleted the fix_a_false_positive_for_style_hash_each_methods branch December 10, 2023 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive on the Style/HashEachMethods cop for array destructuring
3 participants