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

False positive on the Style/HashEachMethods cop for array destructuring #12444

Closed
ydakuka opened this issue Dec 2, 2023 · 5 comments · Fixed by #12508
Closed

False positive on the Style/HashEachMethods cop for array destructuring #12444

ydakuka opened this issue Dec 2, 2023 · 5 comments · Fixed by #12508

Comments

@ydakuka
Copy link

ydakuka commented Dec 2, 2023

Actual behavior

Before autocorrection

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

output:

2.7.6 :002 > [[1, 2, 3], [4, 5, 6]].each { |a, _| puts(a) }
1
4
 => [[1, 2, 3], [4, 5, 6]] 

After autocorrection

def my_method
  [[1, 2, 3], [4, 5, 6]].each_key { |a| puts(a) }
end

output:

2.7.6 :003 > [[1, 2, 3], [4, 5, 6]].each_key { |a| puts(a) }
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):3
NoMethodError (undefined method `each_key' for [[1, 2, 3], [4, 5, 6]]:Array)
Did you mean?  each_entry

Rubocop

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop -V
1.58.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-capybara 2.19.0
  - rubocop-factory_bot 2.24.0
  - rubocop-performance 1.19.1
  - rubocop-rails 2.22.2
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.25.0
  - rubocop-thread_safety 0.5.1
@ydakuka ydakuka changed the title Fix false positive on the Style/HashEachMethods cop False positive on the Style/HashEachMethods cop Dec 2, 2023
@ydakuka ydakuka changed the title False positive on the Style/HashEachMethods cop False positive on the Style/HashEachMethods cop for array destructuring Dec 2, 2023
@ilvez
Copy link

ilvez commented Dec 4, 2023

Thanks for PR, not sure if it got solved but I had issue with the same cop with this:

[1, 2, 3].zip([[4, 5], [5, 6], [6, 7]]).each do |a, (b, c)|
  puts("#{a} - #{b} - #{c}")
end

I could report it as separate issue if needed.

@ydakuka
Copy link
Author

ydakuka commented Dec 4, 2023

ilvez, it will be solved here: #12441

@ilvez
Copy link

ilvez commented Dec 4, 2023

@ydakuka Thank you for quick response!

@postmodern
Copy link

Also came here after getting this blatant false positive:

lib/ronin/support/crypto/cert.rb:407:13: C: [Correctable] Style/HashEachMethods: Use each_key instead of each and remove the unused (value,critical) block argument.
10
            extensions.each do |name,(value,critical)| ...
11
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            extensions.each do |name,(value,critical)|
              ext = extension_factory.create_extension(name,value,critical)
              cert.add_extension(ext)
            end

@pbstriker38
Copy link

False positive here too

[{ 'name' => 'foo' }, { 'name' => 'bar' }].each do |attributes, name: attributes['name']|
  do_something_with_name(name)
end

jmartin-sul added a commit to sul-dlss/was-registrar-app that referenced this issue Dec 5, 2023
koic added a commit to koic/rubocop that referenced this issue Dec 10, 2023
Fixes rubocop#12444.

This PR fixes false positive for `Style/HashEachMethods`
when receiver literal is not a hash literal.
bbatsov pushed a commit that referenced this issue Dec 10, 2023
Fixes #12444.

This PR fixes false positive for `Style/HashEachMethods`
when receiver literal is not a hash literal.
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 a pull request may close this issue.

4 participants