From 60ccbefd1f2efafbdd9e76964690983806b5abab Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 19 Sep 2023 12:29:54 +0900 Subject: [PATCH] Fix a false positive for `RedundantActiveRecordAllMethod` Resolves https://github.com/rubocop/rubocop-rails/pull/1114#issuecomment-1722974569 This PR fixes a false positive for `RedundantActiveRecordAllMethod` when using `find` with block argument. `Enumerable#find` and `ActiveRecord::Base#find` have different arguments, So false positives can be prevented. --- ...rails_redundant_active_record_all_method.md | 1 + .../redundant_active_record_all_method.rb | 8 ++++++++ .../redundant_active_record_all_method_spec.rb | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 changelog/fix_a_false_positive_for_rails_redundant_active_record_all_method.md diff --git a/changelog/fix_a_false_positive_for_rails_redundant_active_record_all_method.md b/changelog/fix_a_false_positive_for_rails_redundant_active_record_all_method.md new file mode 100644 index 0000000000..5f81e19db1 --- /dev/null +++ b/changelog/fix_a_false_positive_for_rails_redundant_active_record_all_method.md @@ -0,0 +1 @@ +* [#1126](https://github.com/rubocop/rubocop-rails/pull/1126): Fix a false positive for `RedundantActiveRecordAllMethod` when using `find` with block argument. ([@koic][]) 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 9349179de3..9d9414704b 100644 --- a/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +++ b/lib/rubocop/cop/rails/redundant_active_record_all_method.rb @@ -134,6 +134,7 @@ class RedundantActiveRecordAllMethod < Base def on_send(node) return unless followed_by_query_method?(node.parent) return if node.receiver.nil? && !inherit_active_record_base?(node) + return if use_find_method_with_block?(node) range_of_all_method = offense_range(node) add_offense(range_of_all_method) do |collector| @@ -144,6 +145,13 @@ def on_send(node) private + def use_find_method_with_block?(node) + parent = node.parent + return false unless parent.method?(:find) + + parent.parent&.block_type? || parent.parent&.numblock_type? || parent.first_argument&.block_pass_type? + end + def offense_range(node) range_between(node.loc.selector.begin_pos, node.source_range.end_pos) end 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 888f734b69..e5cdcbf7ec 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 @@ -387,5 +387,23 @@ class User < ::ActiveRecord::Base end RUBY end + + it 'does not register an offense when using `find` with block' do + expect_no_offenses(<<~RUBY) + User.all.find { |item| item.do_something } + RUBY + end + + it 'does not register an offense when using `find` with numbered block' do + expect_no_offenses(<<~RUBY) + User.all.find { _1.do_something } + RUBY + end + + it 'does not register an offense when using `find` with symbol block' do + expect_no_offenses(<<~RUBY) + User.all.find(&:do_something) + RUBY + end end end