Skip to content

Commit

Permalink
Fix a false negative for Performance/ChainArrayAllocation aware of …
Browse files Browse the repository at this point in the history
…`first(do_something).uniq`

This PR fixes a false negative for `Performance/ChainArrayAllocation`
when using `array.first(do_something).uniq`.
  • Loading branch information
koic committed Jun 15, 2022
1 parent c87ea27 commit 0837b87
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 @@
* [#294](https://github.com/rubocop/rubocop-performance/pull/294): Fix a false negative for `Performance/ChainArrayAllocation` when using `array.first(do_something).uniq`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/chain_array_allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ChainArrayAllocation < Base

def_node_matcher :chain_array_allocation?, <<~PATTERN
(send {
(send _ $%RETURN_NEW_ARRAY_WHEN_ARGS {int lvar ivar cvar gvar})
(send _ $%RETURN_NEW_ARRAY_WHEN_ARGS {int lvar ivar cvar gvar send})
(block (send _ $%ALWAYS_RETURNS_NEW_ARRAY) ...)
(send _ $%RETURNS_NEW_ARRAY ...)
} $%HAS_MUTATION_ALTERNATIVE ...)
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/performance/chain_array_allocation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
^^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
RUBY
end

it 'registers an offense for `first(do_something).uniq`' do
expect_offense(<<~RUBY)
[1, 2, 3, 4].first(do_something).uniq
^^^^^ Use unchained `first` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
RUBY
end
end

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

0 comments on commit 0837b87

Please sign in to comment.