Closed
Description
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
Activity