class RuboCop::Cop::Style::RedundantAssignment

Checks for redundant assignment before returning.

@example

# bad
def test
  x = foo
  x
end

# bad
def test
  if x
    z = foo
    z
  elsif y
    z = bar
    z
  end
end

# good
def test
  foo
end

# good
def test
  if x
    foo
  elsif y
    bar
  end
end

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/style/redundant_assignment.rb, line 50
def on_def(node)
  check_branch(node.body)
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

check_begin_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_assignment.rb, line 99
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
check_branch(node) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/rubocop/cop/style/redundant_assignment.rb, line 58
def check_branch(node)
  return unless node

  case node.type
  when :case       then check_case_node(node)
  when :case_match then check_case_match_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
check_case_match_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_assignment.rb, line 79
def check_case_match_node(node)
  node.in_pattern_branches.each { |in_pattern_node| check_branch(in_pattern_node.body) }
  check_branch(node.else_branch)
end
check_case_node(node) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity

# File lib/rubocop/cop/style/redundant_assignment.rb, line 74
def check_case_node(node)
  node.when_branches.each { |when_node| check_branch(when_node.body) }
  check_branch(node.else_branch)
end
check_ensure_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_assignment.rb, line 95
def check_ensure_node(node)
  check_branch(node.body)
end
check_if_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_assignment.rb, line 84
def check_if_node(node)
  return if node.modifier_form? || node.ternary?

  check_branch(node.if_branch)
  check_branch(node.else_branch)
end
check_rescue_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_assignment.rb, line 91
def check_rescue_node(node)
  node.child_nodes.each { |child_node| check_branch(child_node) }
end