class RuboCop::Cop::Lint::RedundantWithIndex

This cop 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

Constants

MSG_EACH_WITH_INDEX
MSG_WITH_INDEX

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/lint/redundant_with_index.rb, line 47
def autocorrect(node)
  lambda do |corrector|
    redundant_with_index?(node) do |send|
      if send.method_name == :each_with_index
        corrector.replace(send.loc.selector, 'each')
      else
        corrector.remove(send.loc.selector)
        corrector.remove(send.loc.dot)
      end
    end
  end
end
on_block(node) click to toggle source
# File lib/rubocop/cop/lint/redundant_with_index.rb, line 41
def on_block(node)
  redundant_with_index?(node) do |send|
    add_offense(node, location: with_index_range(send))
  end
end

Private Instance Methods

message(node) click to toggle source
# File lib/rubocop/cop/lint/redundant_with_index.rb, line 62
def message(node)
  if node.method_name == :each_with_index
    MSG_EACH_WITH_INDEX
  else
    MSG_WITH_INDEX
  end
end
with_index_range(send) click to toggle source
# File lib/rubocop/cop/lint/redundant_with_index.rb, line 70
def with_index_range(send)
  range_between(send.loc.selector.begin_pos, send.loc.selector.end_pos)
end