Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Enumerable methods called with no receiver in CollectionLiteralInLoop #164

Merged
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
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