-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Performance/HashEachMethods matches non-hashes #4732
Comments
That's unavoidable and unfixable due to the nature of Ruby. We can't really tell apart a hash from something that looks like a hash. You should either disable the cop or simply using |
It doesn't end up being very rare, given that a handful of common Hash methods themselves return an array of 2-length arrays:
|
Not sure what you mean. In the end, because we don't know what something is at runtime, we need to work with trade-offs. I'm inclined to think hashes are more common than arrays of two-element arrays. If it's not the case for your project, then there are tools to disable selectively or altogether. 🙂 |
In 0.50.0 I'm seeing
|
(snapshot.to_a - last_snapshot.to_a).each do |file, _mtime|
@changes[file] = last_snapshot[file] ? :updated : :created
end 😕 |
I'm inclined to make a PR to Rails regarding this, actually. The |
(snapshot.to_a - last_snapshot.to_a).each do |file, _mtime|
@changes[file] = last_snapshot[file] ? :updated : :created
end @AlexWayfer I'd change the code to: (snapshot.to_a - last_snapshot.to_a).each do |(file, _mtime)|
@changes[file] = last_snapshot[file] ? :updated : :created
end I know that's it's more or less the same thing, but I like denoting that I'm explicitly breaking one argument apart when dealing with arrays and this won't be flagged by RuboCop as a bonus. |
Same goes for @akerl's example. |
@bbatsov: Never thought of that. 🤔 I think it's good enough to go in the style guide! |
@bbatsov OK, thank you. |
…ring an offense when `#each` follows `#to_a` This cop would register an offense when `#each` followed `#to_a`, despite only working on hashes. This change fixes that, and also adds a note to the documentation about the possibility of adding parentheses around the arguments to indicate that one is working on a multidimensional array.
Rails `ActionController::Parameters` has no `each_value` method. But RuboCop cannot know that, so it change `each.value` to `each_value` See: rubocop/rubocop#4732
I'm still seeing this issue in rubocop 0.51.0 + rails 5.1.4. I have disabled this cop for now.
Works for me, Ted. Did this PR to rails ever happen? If not, I can probably do it. |
FYI @jaredbeck, for the |
The change in 0.50.0 for Performance/HashEachMethods causes it to match on non-hashes where the block takes 2 args.
Expected behavior
On the following, no cop failure should be reported:
Actual behavior
A failure is reported:
Steps to reproduce the problem
The above code sample should be able to reproduce this
RuboCop version
The text was updated successfully, but these errors were encountered: