class RuboCop::Cop::Lint::RedundantWithObject

This cop checks for redundant `with_object`.

@example

# bad
ary.each_with_object([]) do |v|
  v
end

# good
ary.each do |v|
  v
end

# bad
ary.each.with_object([]) do |v|
  v
end

# good
ary.each do |v|
  v
end

Constants

MSG_EACH_WITH_OBJECT
MSG_WITH_OBJECT

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/lint/redundant_with_object.rb, line 48
def autocorrect(node)
  lambda do |corrector|
    redundant_with_object?(node) do |send|
      if send.method_name == :each_with_object
        corrector.replace(with_object_range(send), 'each')
      else
        corrector.remove(with_object_range(send))
        corrector.remove(send.loc.dot)
      end
    end
  end
end
on_block(node) click to toggle source
# File lib/rubocop/cop/lint/redundant_with_object.rb, line 42
def on_block(node)
  redundant_with_object?(node) do |send|
    add_offense(node, location: with_object_range(send))
  end
end

Private Instance Methods

message(node) click to toggle source
# File lib/rubocop/cop/lint/redundant_with_object.rb, line 63
def message(node)
  if node.method_name == :each_with_object
    MSG_EACH_WITH_OBJECT
  else
    MSG_WITH_OBJECT
  end
end
with_object_range(send) click to toggle source
# File lib/rubocop/cop/lint/redundant_with_object.rb, line 71
def with_object_range(send)
  range_between(
    send.loc.selector.begin_pos,
    send.source.length
  )
end