Skip to content

Commit 0837b87

Browse files
committed
Fix a false negative for Performance/ChainArrayAllocation aware of first(do_something).uniq
This PR fixes a false negative for `Performance/ChainArrayAllocation` when using `array.first(do_something).uniq`.
1 parent c87ea27 commit 0837b87

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+
* [#294](https://github.com/rubocop/rubocop-performance/pull/294): Fix a false negative for `Performance/ChainArrayAllocation` when using `array.first(do_something).uniq`. ([@koic][])

lib/rubocop/cop/performance/chain_array_allocation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ChainArrayAllocation < Base
5353

5454
def_node_matcher :chain_array_allocation?, <<~PATTERN
5555
(send {
56-
(send _ $%RETURN_NEW_ARRAY_WHEN_ARGS {int lvar ivar cvar gvar})
56+
(send _ $%RETURN_NEW_ARRAY_WHEN_ARGS {int lvar ivar cvar gvar send})
5757
(block (send _ $%ALWAYS_RETURNS_NEW_ARRAY) ...)
5858
(send _ $%RETURNS_NEW_ARRAY ...)
5959
} $%HAS_MUTATION_ALTERNATIVE ...)

spec/rubocop/cop/performance/chain_array_allocation_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
^^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
2424
RUBY
2525
end
26+
27+
it 'registers an offense for `first(do_something).uniq`' do
28+
expect_offense(<<~RUBY)
29+
[1, 2, 3, 4].first(do_something).uniq
30+
^^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
31+
RUBY
32+
end
2633
end
2734

2835
describe 'methods that only return an array with no block' do

0 commit comments

Comments
 (0)