Skip to content

Commit

Permalink
Merge pull request #164 from eugeneius/collection_literal_in_loop_no_…
Browse files Browse the repository at this point in the history
…receiver

Handle Enumerable methods called with no receiver in CollectionLiteralInLoop
  • Loading branch information
koic authored Sep 5, 2020
2 parents 37a964b + 1c23eba commit 5da259b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#164](https://github.com/rubocop-hq/rubocop-performance/pull/164): Fix an error for `Performance/CollectionLiteralInLoop` when a method from `Enumerable` is called with no receiver. ([@eugeneius][])

## 1.8.0 (2020-09-04)

### New features
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/collection_literal_in_loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def keyword_loop?(type)

def node_within_enumerable_loop?(node, ancestor)
enumerable_loop?(ancestor) do |receiver|
receiver != node && !receiver.descendants.include?(node)
receiver != node && !receiver&.descendants&.include?(node)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@
end
RUBY
end

it 'registers an offense when the method is called with no receiver' do
expect_offense(<<~RUBY)
all? do |e|
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
end

context 'when not inside loop' do
Expand Down

0 comments on commit 5da259b

Please sign in to comment.