class RubbyCop::Cop::Style::RedundantReturn

This cop checks for redundant `return` expressions.

@example

def test
  return something
end

def test
  one
  two
  three
  return something
end

It should be extended to handle methods whose body is if/else or a case expression with a default branch.

Constants

MSG

Private Instance Methods

add_braces(corrector, node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 54
def add_braces(corrector, node)
  kids = node.children.map(&:source_range)
  corrector.insert_before(kids.first, '{')
  corrector.insert_after(kids.last, '}')
end
add_brackets(corrector, node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 48
def add_brackets(corrector, node)
  kids = node.children.map(&:source_range)
  corrector.insert_before(kids.first, '[')
  corrector.insert_after(kids.last, ']')
end
arguments?(args) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 60
def arguments?(args)
  return false if args.empty?
  return true if args.size > 1

  !args.first.begin_type? || !args.first.children.empty?
end
autocorrect(node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 30
def autocorrect(node)
  lambda do |corrector|
    unless arguments?(node.children)
      corrector.replace(node.source_range, 'nil')
      next
    end

    return_value, = *node
    if node.children.size > 1
      add_brackets(corrector, node)
    elsif return_value.hash_type?
      add_braces(corrector, return_value) unless return_value.braces?
    end
    return_kw = range_with_surrounding_space(node.loc.keyword, :right)
    corrector.remove(return_kw)
  end
end
check_begin_node(node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 110
def check_begin_node(node)
  expressions = *node
  last_expr = expressions.last

  return unless last_expr && last_expr.return_type?

  check_return_node(last_expr)
end
check_branch(node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 73
def check_branch(node)
  case node.type
  when :return then check_return_node(node)
  when :case then check_case_node(node)
  when :if then check_if_node(node)
  when :begin then check_begin_node(node)
  end
end
check_case_node(node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 89
def check_case_node(node)
  _cond, *when_nodes, else_node = *node
  when_nodes.each { |when_node| check_when_node(when_node) }
  check_branch(else_node) if else_node
end
check_if_node(node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 101
def check_if_node(node)
  return if node.modifier_form? || node.ternary?

  _cond, if_node, else_node = *node.node_parts

  check_branch(if_node) if if_node
  check_branch(else_node) if else_node
end
check_return_node(node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 82
def check_return_node(node)
  return if cop_config['AllowMultipleReturnValues'] &&
            node.children.size > 1

  add_offense(node, :keyword)
end
check_when_node(node) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 95
def check_when_node(node)
  return unless node
  _cond, body = *node
  check_branch(body) if body
end
on_method_def(_node, _method_name, _args, body) click to toggle source
# File lib/rubbycop/cop/style/redundant_return.rb, line 67
def on_method_def(_node, _method_name, _args, body)
  return unless body

  check_branch(body)
end