Class: RuboCop::Cop::Performance::ReverseEach
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
RangeHelp,
Base
|
|
Inherits: |
Base
|
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.
[1, 2, 3].reverse.each {} #=> [3, 2, 1]
[1, 2, 3].reverse_each {} #=> [1, 2, 3]
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/reverse_each.rb', line 26'Use `reverse_each` instead of `reverse.each`.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/reverse_each.rb', line 27%i[each].freeze
Instance Method Summary
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
- #offense_range(node) private
- #use_return_value?(node) ⇒ Boolean private
Instance Method Details
#offense_range(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/reverse_each.rb', line 54
def offense_range(node) range_between(node.children.first.loc.selector.begin_pos, node.loc.selector.end_pos) end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/performance/reverse_each.rb', line 44
alias on_csend on_send
#on_send(node) Also known as: #on_csend
[ GitHub ]# 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
#use_return_value?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/performance/reverse_each.rb', line 48
def use_return_value?(node) !!node.ancestors.detect do |ancestor| ancestor.assignment? || ancestor.send_type? || ancestor.return_type? end end