class RuboCop::Cop::Style::RedundantAssignment


end
end
bar
elsif y
foo
if x
def test
# good
end
foo
def test
# good
end
end
z
z = bar
elsif y
z
z = foo
if x
def test
# bad
end
x
x = foo
def test
# bad
@example
This cop checks for redundant assignment before returning.

def check_begin_node(node)

def check_begin_node(node)
  if (assignment = redundant_assignment?(node))
    add_offense(assignment) do |corrector|
      expression = assignment.children[1]
      corrector.replace(assignment, expression.source)
      corrector.remove(assignment.right_sibling)
    end
  else
    last_expr = node.children.last
    check_branch(last_expr)
  end
end

def check_branch(node)

def check_branch(node)
  return unless node
  case node.type
  when :case   then check_case_node(node)
  when :if     then check_if_node(node)
  when :rescue, :resbody
    check_rescue_node(node)
  when :ensure then check_ensure_node(node)
  when :begin, :kwbegin
    check_begin_node(node)
  end
end

def check_case_node(node)

def check_case_node(node)
  node.when_branches.each { |when_node| check_branch(when_node.body) }
  check_branch(node.else_branch)
end

def check_ensure_node(node)

def check_ensure_node(node)
  check_branch(node.body)
end

def check_if_node(node)

def check_if_node(node)
  return if node.modifier_form? || node.ternary?
  check_branch(node.if_branch)
  check_branch(node.else_branch)
end

def check_rescue_node(node)

def check_rescue_node(node)
  node.child_nodes.each { |child_node| check_branch(child_node) }
end

def on_def(node)

def on_def(node)
  check_branch(node.body)
end