Skip to content

Commit 2d7e7a0

Browse files
committed
Fix false negatives for Rails/RedundantActiveRecordAllMethod when using all method in block
This PR fixes false negatives for `Rails/RedundantActiveRecordAllMethod` when methods in `POSSIBLE_ENUMERABLE_BLOCK_METHODS` are used in a block as following: ``` expect { subject }.to change { User.all.count } ```
1 parent 1d7f0c4 commit 2d7e7a0

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1382](https://github.com/rubocop/rubocop-rails/pull/1382): Fix false negatives for `Rails/RedundantActiveRecordAllMethod` when using `all` method in block. ([@masato-bkn][])

lib/rubocop/cop/rails/redundant_active_record_all_method.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def possible_enumerable_block_method?(node)
174174
parent = node.parent
175175
return false unless POSSIBLE_ENUMERABLE_BLOCK_METHODS.include?(parent.method_name)
176176

177-
parent.parent&.block_type? || parent.parent&.numblock_type? || parent.first_argument&.block_pass_type?
177+
parent.block_literal? || parent.first_argument&.block_pass_type?
178178
end
179179

180180
def sensitive_association_method?(node)

spec/rubocop/cop/rails/redundant_active_record_all_method_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@
334334
User.all.#{method}(&:do_something)
335335
RUBY
336336
end
337+
338+
it "registers an offense when using `#{method}` in block" do
339+
expect_offense(<<~RUBY)
340+
do_something { User.all.#{method} }
341+
^^^ Redundant `all` detected.
342+
RUBY
343+
end
337344
end
338345
end
339346

0 commit comments

Comments
 (0)