class Rubocop::Cop::Lint::UselessAssignment

rescue, ensure, etc.
reassignments and properly handles varied cases such as branch, loop,
Currently this cop has advanced logic that detects unreferenced
assigned but unused variable - foo
The basic idea for this cop was from the warning of ‘ruby -cw`:
scope.
This cop checks for every useless assignment to local variable in every

def after_leaving_scope(scope)

def after_leaving_scope(scope)
  scope.variables.each_value do |variable|
    check_for_unused_assignments(variable)
    check_for_unused_block_local_variable(variable)
  end
end

def check_for_unused_assignments(variable)

def check_for_unused_assignments(variable)
  return if variable.name.to_s.start_with?('_')
  variable.assignments.each do |assignment|
    next if assignment.used?
    message = message_for_useless_assignment(assignment)
    location = if assignment.regexp_named_capture?
                 assignment.node.children.first.loc.expression
               else
                 assignment.node.loc.name
               end
    warning(nil, location, message)
  end
end

def check_for_unused_block_local_variable(variable)

def check_for_unused_block_local_variable(variable)
  return unless variable.block_local_variable?
  return unless variable.assignments.empty?
  message = sprintf(MSG, variable.name)
  warning(variable.declaration_node, :expression, message)
end

def investigate(processed_source)

def investigate(processed_source)
  inspect_variables(processed_source.ast)
end

def message_for_useless_assignment(assignment)

def message_for_useless_assignment(assignment)
  variable = assignment.variable
  message = sprintf(MSG, variable.name)
  if assignment.multiple_assignment?
    message << ". Use _ or _#{variable.name} as a variable name " +
               "to indicate that it won't be used."
  elsif assignment.operator_assignment?
    return_value_node = return_value_node_of_scope(variable.scope)
    if assignment.meta_assignment_node.equal?(return_value_node)
      non_assignment_operator = assignment.operator.sub(/=$/, '')
      message << ". Use just operator #{non_assignment_operator}."
    end
  end
  message
end

def return_value_node_of_scope(scope)

TODO: More precise handling (rescue, ensure, nested begin, etc.)
def return_value_node_of_scope(scope)
  body_node = scope.body_node
  if body_node.type == :begin
    body_node.children.last
  else
    body_node
  end
end