Sha256: 7df0852fa693507e7e58963c0325be651c2b70f9f327edd36525c02b595ac33e

Contents?: true

Size: 1.52 KB

Versions: 74

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # 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]
      # ----
      #
      # @example
      #   # bad
      #   items.reverse.each
      #
      #   # good
      #   items.reverse_each
      class ReverseEach < Base
        include RangeHelp
        extend AutoCorrector

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

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

        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
        alias on_csend on_send

        private

        def use_return_value?(node)
          !!node.ancestors.detect do |ancestor|
            ancestor.assignment? || ancestor.send_type? || ancestor.return_type?
          end
        end

        def offense_range(node)
          range_between(node.children.first.loc.selector.begin_pos, node.loc.selector.end_pos)
        end
      end
    end
  end
end

Version data entries

74 entries across 74 versions & 2 rubygems

Version Path
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/reverse_each.rb