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 positives in Rails/Pluck #833

Closed
cover opened this issue Oct 25, 2022 · 0 comments
Closed

False positives in Rails/Pluck #833

cover opened this issue Oct 25, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@cover
Copy link

cover commented Oct 25, 2022


Expected behavior

The cop should not suggest to use pluck when there are multiple block arguments or when the argument is used inside the block.

Actual behavior

Rubocop is suggesting to use pluck, which is not the correct solution (the code would break). Likely related with #831 (/cc @koic)

Steps to reproduce the problem

Ignore the non-sense example, it's mainly to show the 2 wrong suggestions on the two map:

class Test
  def test
    id_values = { 10 => 'foo', 20 => 'bar', 30 => 'baz' }

    items = {}
    50.times do |i|
      items[i] = { 'condition' => rand(2).zero?, 'id' => i }
    end

    true_items, _false_items = items.partition { |_, item| item['condition'] }

    true_items_ids = true_items.map { |_, obj| obj['id'] }

    true_items_ids.map { |id| id_values[id] }
  end
end
Offenses:

test.rb:12:33: C: [Correctable] Rails/Pluck: Prefer pluck('id') over map { |_, obj| obj['id'] }.
    true_items_ids = true_items.map { |_, obj| obj['id'] }
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:14:20: C: [Correctable] Rails/Pluck: Prefer pluck(id) over map { |id| id_values[id] }.
    true_items_ids.map { |id| id_values[id] }
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses autocorrectable

RuboCop version

1.37.0 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.2) [x86_64-linux]
  - rubocop-rails 2.17.1
@koic koic added the bug Something isn't working label Oct 25, 2022
@koic koic closed this as completed in 85d615f Oct 26, 2022
koic added a commit that referenced this issue Oct 26, 2022
Fixes #833.

This commit fixes a false positive for `Rails/Pluck` when receiver is not block argument for `[]`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants