Skip to content

Commit fbc59de

Browse files
committed
[Fix rubocop#833] Fix a false positive for Rails/Pluck
Fixes rubocop#833. This commit fixes a false positive for `Rails/Pluck` when receiver is not block argument for `[]`.
1 parent 85d615f commit fbc59de

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#833](https://github.com/rubocop/rubocop-rails/issues/833): Fix a false positive for `Rails/Pluck` when receiver is not block argument for `[]`. ([@koic][])

lib/rubocop/cop/rails/pluck.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def use_one_block_argument?(argument)
5555
end
5656

5757
def use_block_argument_in_key?(block_argument, key)
58+
return false if block_argument == key.source
59+
5860
key.each_descendant(:lvar).none? { |lvar| block_argument == lvar.source }
5961
end
6062

spec/rubocop/cop/rails/pluck_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
end
5959
end
6060

61+
context 'when receiver is not block argument for `[]`' do
62+
it 'does not register an offense' do
63+
expect_no_offenses(<<~RUBY)
64+
lvar = do_something
65+
x.#{method} { |id| lvar[id] }
66+
RUBY
67+
end
68+
end
69+
6170
context 'when there are multiple block arguments' do
6271
it 'does not register an offense' do
6372
expect_no_offenses(<<~RUBY)

0 commit comments

Comments
 (0)