diff --git a/changelog/fix_fix_false_negatives_for_ rails_redundant_active_record_all_method.md b/changelog/fix_fix_false_negatives_for_ rails_redundant_active_record_all_method.md new file mode 100644 index 0000000000..c77140b269 --- /dev/null +++ b/changelog/fix_fix_false_negatives_for_ rails_redundant_active_record_all_method.md @@ -0,0 +1 @@ +* [#1382](https://github.com/rubocop/rubocop-rails/pull/1382): Fix false negatives for `Rails/RedundantActiveRecordAllMethod` when using `all` method in block. ([@masato-bkn][]) diff --git a/lib/rubocop/cop/rails/redundant_active_record_all_method.rb b/lib/rubocop/cop/rails/redundant_active_record_all_method.rb index 252e890875..cf8599ad42 100644 --- a/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +++ b/lib/rubocop/cop/rails/redundant_active_record_all_method.rb @@ -174,7 +174,7 @@ def possible_enumerable_block_method?(node) parent = node.parent return false unless POSSIBLE_ENUMERABLE_BLOCK_METHODS.include?(parent.method_name) - parent.parent&.block_type? || parent.parent&.numblock_type? || parent.first_argument&.block_pass_type? + parent.block_literal? || parent.first_argument&.block_pass_type? end def sensitive_association_method?(node) diff --git a/spec/rubocop/cop/rails/redundant_active_record_all_method_spec.rb b/spec/rubocop/cop/rails/redundant_active_record_all_method_spec.rb index 43bd1b89fa..5b7e35bd48 100644 --- a/spec/rubocop/cop/rails/redundant_active_record_all_method_spec.rb +++ b/spec/rubocop/cop/rails/redundant_active_record_all_method_spec.rb @@ -334,6 +334,13 @@ User.all.#{method}(&:do_something) RUBY end + + it "registers an offense when using `#{method}` in block" do + expect_offense(<<~RUBY) + do_something { User.all.#{method} } + ^^^ Redundant `all` detected. + RUBY + end end end