Skip to content

Commit

Permalink
Merge pull request #392 from fatkodima/reverse_each-more-safe-navigation
Browse files Browse the repository at this point in the history
Fix a false negative for `Performance/ReverseEach` when safe navigation is between `reverse` and `each`
  • Loading branch information
koic authored Nov 26, 2023
2 parents eb714d9 + 761025b commit c5bc8f4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_false_negative_for_reverse_each.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#390](https://github.com/rubocop/rubocop-performance/issues/390): Fix a false negative for `Performance/ReverseEach` when safe navigation is between `reverse` and `each`. ([@fatkodima][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/reverse_each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ReverseEach < Base
RESTRICT_ON_SEND = %i[each].freeze

def_node_matcher :reverse_each?, <<~MATCHER
(send (call _ :reverse) :each)
(call (call _ :reverse) :each)
MATCHER

def on_send(node)
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/performance/reverse_each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
RUBY
end

it 'registers an offense when each is called on reverse with safe navigation operator chain' do
expect_offense(<<~RUBY)
array&.reverse&.each { |e| puts e }
^^^^^^^^^^^^^ Use `reverse_each` instead of `reverse.each`.
RUBY
end

it 'registers an offense when each is called on reverse on a variable' do
expect_offense(<<~RUBY)
arr = [1, 2, 3]
Expand Down

0 comments on commit c5bc8f4

Please sign in to comment.