Skip to content

Commit

Permalink
Merge pull request #1459 from Earlopain/uniq-before-pluck-numblock
Browse files Browse the repository at this point in the history
Fix a false positive for `Rails/UniqBeforePluck` when using a numblock with `uniq`
  • Loading branch information
koic authored Feb 28, 2025
2 parents 67cff82 + 78988e7 commit a250bfa
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_false_positive_uniq_before_pluck_numblock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1459](https://github.com/rubocop/rubocop-rails/pull/1459): Fix a false positive for `Rails/UniqBeforePluck` when using a numblock with `uniq`. ([@earlopain][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/uniq_before_pluck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class UniqBeforePluck < Base
MSG = 'Use `distinct` before `pluck`.'
RESTRICT_ON_SEND = %i[uniq].freeze

def_node_matcher :uniq_before_pluck, '[!^block $(send $(send _ :pluck ...) :uniq ...)]'
def_node_matcher :uniq_before_pluck, '[!^any_block $(send $(send _ :pluck ...) :uniq ...)]'

def on_send(node)
uniq_before_pluck(node) do |uniq_node, pluck_node|
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/uniq_before_pluck_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
Model.where(foo: 1).pluck(:name).uniq { |k| k[0] }
RUBY
end

it 'ignores uniq with a numblock' do
expect_no_offenses(<<~RUBY)
Model.where(foo: 1).pluck(:name).uniq { _1[0] }
RUBY
end
end

it 'registers an offense' do
Expand Down

0 comments on commit a250bfa

Please sign in to comment.