diff --git a/changelog/fix_false_positive_for_rails_redundant_active_record_all_method_when_all_has_any_parameters.md b/changelog/fix_false_positive_for_rails_redundant_active_record_all_method_when_all_has_any_parameters.md new file mode 100644 index 0000000000..d687861339 --- /dev/null +++ b/changelog/fix_false_positive_for_rails_redundant_active_record_all_method_when_all_has_any_parameters.md @@ -0,0 +1 @@ +* [#1110](https://github.com/rubocop/rubocop-rails/issues/1110): Fix false positive for `Rails/RedundantActiveRecordAllMethod` when `all` has any parameters. ([@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 3d7e171f1e..afd9c7094a 100644 --- a/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +++ b/lib/rubocop/cop/rails/redundant_active_record_all_method.rb @@ -127,7 +127,7 @@ class RedundantActiveRecordAllMethod < Base ].to_set.freeze def_node_matcher :followed_by_query_method?, <<~PATTERN - (send (send _ :all ...) QUERYING_METHODS ...) + (send (send _ :all) QUERYING_METHODS ...) PATTERN def on_send(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 c009fbcd3f..06231bd5fb 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 @@ -260,6 +260,26 @@ RUBY end end + + context 'when `all` has any parameters (=not `all` is not Active Record method)' do + it 'does not register an offense when no method follows `all`' do + expect_no_offenses(<<~RUBY) + page.all(:parameter) + RUBY + end + + it 'does not register an offense when method follows `all`' do + expect_no_offenses(<<~RUBY) + page.all(:parameter).do_something + RUBY + end + + it 'does not register an offense when method from `ActiveRecord::Querying::QUERYING_METHODS` follows `all`' do + expect_no_offenses(<<~RUBY) + page.all(:parameter).select(some_filter) + RUBY + end + end end context 'with no receiver' do