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

Fix a false positive for Rails/UniqBeforePluck when using a numblock with uniq #1459

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading