-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[Fix #12444] Fix false positive for Style/HashEachMethods
#12508
Conversation
0927724
to
aa1d624
Compare
|
||
it "does not register an offense when `['array', 'literal'].keys.each`" do | ||
expect_no_offenses(<<~RUBY) | ||
['array', 'literal'].keys.each { |k| p k } |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
f5ad65b
to
95d0e46
Compare
@@ -88,6 +90,12 @@ def on_block_pass(node) | |||
|
|||
private | |||
|
|||
def handlable?(node) |
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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.
95d0e46
to
23a9f6f
Compare
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:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.