Class: RuboCop::Cop::Performance::ReverseEach

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/performance/reverse_each.rb

Overview

Identifies usages of ‘reverse.each` and change them to use `reverse_each` instead.

If the return value is used, it will not be detected because the result will be different.

source,ruby

1, 2, 3].reverse.each {} #=> [3, 2, 1
1, 2, 3].reverse_each {} #=> [1, 2, 3

Examples:

# bad
items.reverse.each

# good
items.reverse_each

Constant Summary collapse

MSG =
'Use `reverse_each` instead of `reverse.each`.'
RESTRICT_ON_SEND =
%i[each].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/performance/reverse_each.rb', line 33

def on_send(node)
  return if use_return_value?(node)

  reverse_each?(node) do
    range = offense_range(node)

    add_offense(range) do |corrector|
      corrector.replace(range, 'reverse_each')
    end
  end
end