Skip to content

Commit

Permalink
Fix false negatives for Rails/RedundantActiveRecordAllMethod when u…
Browse files Browse the repository at this point in the history
…sing `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 }
```
  • Loading branch information
masato-bkn committed Oct 29, 2024
1 parent 1d7f0c4 commit 2d7e7a0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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][])
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 2d7e7a0

Please sign in to comment.