From 78988e779796c2f7f372e0cdb0b52ab286f77ac0 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Fri, 28 Feb 2025 15:11:54 +0100 Subject: [PATCH] Fix a false positive for `Rails/UniqBeforePluck` when using a numblock with `uniq` --- changelog/fix_false_positive_uniq_before_pluck_numblock.md | 1 + lib/rubocop/cop/rails/uniq_before_pluck.rb | 2 +- spec/rubocop/cop/rails/uniq_before_pluck_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positive_uniq_before_pluck_numblock.md diff --git a/changelog/fix_false_positive_uniq_before_pluck_numblock.md b/changelog/fix_false_positive_uniq_before_pluck_numblock.md new file mode 100644 index 0000000000..20388750b6 --- /dev/null +++ b/changelog/fix_false_positive_uniq_before_pluck_numblock.md @@ -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][]) diff --git a/lib/rubocop/cop/rails/uniq_before_pluck.rb b/lib/rubocop/cop/rails/uniq_before_pluck.rb index 323ace2694..c369789cab 100644 --- a/lib/rubocop/cop/rails/uniq_before_pluck.rb +++ b/lib/rubocop/cop/rails/uniq_before_pluck.rb @@ -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| diff --git a/spec/rubocop/cop/rails/uniq_before_pluck_spec.rb b/spec/rubocop/cop/rails/uniq_before_pluck_spec.rb index 959c36c12b..4704bec946 100644 --- a/spec/rubocop/cop/rails/uniq_before_pluck_spec.rb +++ b/spec/rubocop/cop/rails/uniq_before_pluck_spec.rb @@ -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