Sha256: 82d8aef7ba95f252689ce40ae8dc3483f7f1bff588bc187f97689f2ccd842298

Contents?: true

Size: 1.83 KB

Versions: 118

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for redundant `with_index`.
      #
      # @example
      #   # bad
      #   ary.each_with_index do |v|
      #     v
      #   end
      #
      #   # good
      #   ary.each do |v|
      #     v
      #   end
      #
      #   # bad
      #   ary.each.with_index do |v|
      #     v
      #   end
      #
      #   # good
      #   ary.each do |v|
      #     v
      #   end
      #
      class RedundantWithIndex < Base
        include RangeHelp
        extend AutoCorrector

        MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'
        MSG_WITH_INDEX = 'Remove redundant `with_index`.'

        def on_block(node)
          return unless (send = redundant_with_index?(node))

          range = with_index_range(send)

          add_offense(range, message: message(send)) do |corrector|
            if send.method?(:each_with_index)
              corrector.replace(send.loc.selector, 'each')
            else
              corrector.remove(range)
              corrector.remove(send.loc.dot)
            end
          end
        end

        alias on_numblock on_block

        private

        # @!method redundant_with_index?(node)
        def_node_matcher :redundant_with_index?, <<~PATTERN
          {
            (block
              $(send _ {:each_with_index :with_index} ...)
              (args (arg _)) ...)
            (numblock
              $(send _ {:each_with_index :with_index} ...) 1 ...)
          }
        PATTERN

        def message(node)
          if node.method?(:each_with_index)
            MSG_EACH_WITH_INDEX
          else
            MSG_WITH_INDEX
          end
        end

        def with_index_range(send)
          range_between(send.loc.selector.begin_pos, send.source_range.end_pos)
        end
      end
    end
  end
end

Version data entries

118 entries across 117 versions & 11 rubygems

Version Path
rubocop-1.57.2 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.57.1 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.57.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.56.4 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.56.3 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.56.2 lib/rubocop/cop/lint/redundant_with_index.rb
synctera_ruby_sdk-1.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_with_index.rb
synctera_ruby_sdk-1.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_with_index.rb
synctera_ruby_sdk-1.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_with_index.rb
sampero-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.1/lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.56.1 lib/rubocop/cop/lint/redundant_with_index.rb
tursodb-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_with_index.rb
synctera_ruby_sdk-1.0.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.56.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.55.1 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.55.0 lib/rubocop/cop/lint/redundant_with_index.rb
rubocop-1.54.2 lib/rubocop/cop/lint/redundant_with_index.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/lint/redundant_with_index.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/lint/redundant_with_index.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/lint/redundant_with_index.rb