Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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