class RuboCop::Cop::Style::EachWithObject

1, 2].each_with_object({}) { |e, a| a[e

e }
# good
[1, 2].inject({}) { |a, e| a = e; a }
# bad
@example
parameter is assigned to within the block.
However, we can’t replace with each_with_object if the accumulator
the need to return the object at the end.
returned at the end and so could be replaced by each_with_object without
This cop looks for inject / reduce calls where the passed in object is

def accumulator_param_assigned_to?(body, args)

then we can't convert to each_with_object
if the accumulator parameter is assigned to in the block,
def accumulator_param_assigned_to?(body, args)
  first_arg, = *args
  accumulator_var, = *first_arg
  body.each_descendant.any? do |n|
    next unless n.assignment?
    lhs, _rhs = *n
    lhs.equal?(accumulator_var)
  end
end

def first_argument_returned?(args, return_value)

def first_argument_returned?(args, return_value)
  first_arg, = *args
  accumulator_var, = *first_arg
  return_var, = *return_value
  accumulator_var == return_var
end

def on_block(node)

def on_block(node)
  method, args, body = *node
  return unless reduce_method?(method)
  _, method_name, method_arg = *method
  return if simple_method_arg?(method_arg)
  return_value = return_value(body)
  return unless return_value
  return unless first_argument_returned?(args, return_value)
  return if accumulator_param_assigned_to?(body, args)
  add_offense(method, :selector, format(MSG, method_name))
end

def reduce_method?(method)

def reduce_method?(method)
  return false unless method.send_type?
  _, method_name, _method_arg = *method
  METHODS.include? method_name
end

def return_value(body)

def return_value(body)
  return unless body
  return_value = body.type == :begin ? body.children.last : body
  return_value if return_value && return_value.type == :lvar
end

def simple_method_arg?(method_arg)

def simple_method_arg?(method_arg)
  method_arg && method_arg.basic_literal?
end