Skip to content

Commit e1b1fff

Browse files
authored
Merge pull request #1126 from koic/fix_a_false_positive_for_rails_redundant_active_record_all_method
Fix a false positive for `Rails/RedundantActiveRecordAllMethod`
2 parents 0a57811 + e586f7f commit e1b1fff

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1126](https://github.com/rubocop/rubocop-rails/pull/1126): Fix a false positive for `Rails/RedundantActiveRecordAllMethod` when using some `Enumerable`'s methods with block argument. ([@koic][])

lib/rubocop/cop/rails/redundant_active_record_all_method.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ class RedundantActiveRecordAllMethod < Base
133133
without
134134
].to_set.freeze
135135

136+
POSSIBLE_ENUMERABLE_BLOCK_METHODS = %i[any? count find none? one? select sum].freeze
137+
136138
def_node_matcher :followed_by_query_method?, <<~PATTERN
137139
(send (send _ :all) QUERYING_METHODS ...)
138140
PATTERN
139141

140142
def on_send(node)
141-
return unless followed_by_query_method?(node.parent)
143+
return if !followed_by_query_method?(node.parent) || possible_enumerable_block_method?(node)
142144
return if node.receiver ? allowed_receiver?(node.receiver) : !inherit_active_record_base?(node)
143145

144146
range_of_all_method = offense_range(node)
@@ -150,6 +152,13 @@ def on_send(node)
150152

151153
private
152154

155+
def possible_enumerable_block_method?(node)
156+
parent = node.parent
157+
return false unless POSSIBLE_ENUMERABLE_BLOCK_METHODS.include?(parent.method_name)
158+
159+
parent.parent&.block_type? || parent.parent&.numblock_type? || parent.first_argument&.block_pass_type?
160+
end
161+
153162
def offense_range(node)
154163
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
155164
end

spec/rubocop/cop/rails/redundant_active_record_all_method_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,48 @@ class User < ::ActiveRecord::Base
387387
end
388388
RUBY
389389
end
390+
391+
context 'using `any?`' do
392+
it 'does not register an offense when using `any?` with block' do
393+
expect_no_offenses(<<~RUBY)
394+
User.all.any? { |item| item.do_something }
395+
RUBY
396+
end
397+
398+
it 'does not register an offense when using `any?` with numbered block' do
399+
expect_no_offenses(<<~RUBY)
400+
User.all.any? { _1.do_something }
401+
RUBY
402+
end
403+
404+
it 'does not register an offense when using `any?` with symbol block' do
405+
expect_no_offenses(<<~RUBY)
406+
User.all.any?(&:do_something)
407+
RUBY
408+
end
409+
end
410+
411+
described_class::POSSIBLE_ENUMERABLE_BLOCK_METHODS.each do |method|
412+
context "using `#{method}`" do
413+
it "does not register an offense when using `#{method}` with block" do
414+
expect_no_offenses(<<~RUBY)
415+
User.all.#{method} { |item| item.do_something }
416+
RUBY
417+
end
418+
419+
it "does not register an offense when using `#{method}` with numbered block" do
420+
expect_no_offenses(<<~RUBY)
421+
User.all.#{method} { _1.do_something }
422+
RUBY
423+
end
424+
425+
it "does not register an offense when using `#{method}` with symbol block" do
426+
expect_no_offenses(<<~RUBY)
427+
User.all.#{method}(&:do_something)
428+
RUBY
429+
end
430+
end
431+
end
390432
end
391433

392434
context 'with `AllowedReceivers` config' do

0 commit comments

Comments
 (0)