-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Rails/PluckId is not excluding array collections #301
Comments
The On the other hand, it is difficult to know a receiver type in the current static analysis implementation. So, It may be possible to consider disable the cop by default if there are many false alarms to users. |
Sure, I've disabled the cop because otherwise I have to add a lot of disable comments when using Maybe you can check if it responds to |
Yeah, it would be redundant to introduce |
What do you mean? I have the cop disabled because otherwise it will not pass the CI rubocop check. An alternative could be to add a |
Ah, I was typo. Correctly it is |
Fixes rubocop#301. This PR sets disabled by default for `Rails/PluckId`. An array object does not support `ids` method, that only works for an AR object. So, this PR defaults to prevent false alarms cause by it.
I've opened the PR #307. |
Won't the same problem occur for Rails/PluckInWhere ?
|
@inkstak Sure. There will be similar problems. On the other hand, the context is narrowed down to the condition of |
…luck_id [Fix #301] Set disabled by default for `Rails/PluckId`
Just FYI, I opened the PR #317 to resolve a false positive for |
You can use the
|
@mos-adebayo It won't because
array = [{ id: 1 }, { id: 2 }]
array.pluck(:id) # => [1, 2]
array.map(&:id) # => undefined method `id' for {:id=>1}:Hash (NoMethodError)
Item = Data.define(:id)
array = [Item.new(1), Item.new(2)]
array.map(&:id) #=> [1, 2]
array.pluck(:id) #=> undefined method `[]' for #<data Item id=1> (NoMethodError) |
This cop was originally disabled with the original rubocop import: standardrb@71e48d5#diff-da3213a24b350fa386ff3bab0d6b239112408c3fe4f986cbd9d5236f7e0b61e4R458-R463 It was subsequently enabled during a mass enable commit: standardrb@24bc50f#diff-da3213a24b350fa386ff3bab0d6b239112408c3fe4f986cbd9d5236f7e0b61e4R248-R249 As discussed on this issue, the cop is unsafe by default and frequently gets false positives: rubocop/rubocop-rails#301 It is disabled by default in most recent rubocop release, for same reasons: https://github.com/rubocop/rubocop-rails/blob/v2.25.0/config/default.yml#L746-L751 Discussion of why to disable on: standardrb#31
Expected behavior
No alerts for arrays.
Actual behavior
Rails/PluckId raises an alert when using
pluck(:id)
with array collections.Arrays does not support
.ids
, that only works forActiveRecord::Relation
collections.Steps to reproduce the problem
I have this code:
Rubocop suggestion:
Updated code:
Then it produces the following error:
RuboCop version
The text was updated successfully, but these errors were encountered: