class RuboCop::Cop::Style::RedundantReturn

This cop checks for redundant `return` expressions.

@example

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

# bad
def test
  return something
end

# bad
def test
  one
  two
  three
  return something
end

# good
def test
  return something if something_else
end

# good
def test
  if x
  elsif y
  else
  end
end

Constants

MSG
MULTI_RETURN_MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 51
def autocorrect(node) # rubocop:disable Metrics/MethodLength
  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(range: node.loc.keyword,
                                             side: :right)
    corrector.remove(return_kw)
  end
end
on_def(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 44
def on_def(node)
  return unless node.body

  check_branch(node.body)
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

add_braces(corrector, node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 78
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/rubocop/cop/style/redundant_return.rb, line 72
def add_brackets(corrector, node)
  kids = node.children.map(&:source_range)
  corrector.insert_before(kids.first, '[')
  corrector.insert_after(kids.last, ']')
end
allow_multiple_return_values?() click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 147
def allow_multiple_return_values?
  cop_config['AllowMultipleReturnValues'] || false
end
arguments?(args) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 84
def arguments?(args)
  return false if args.empty?
  return true if args.size > 1

  !args.first.begin_type? || !args.first.children.empty?
end
check_begin_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 138
def check_begin_node(node)
  expressions = *node
  last_expr = expressions.last

  return unless last_expr&.return_type?

  check_return_node(last_expr)
end
check_branch(node) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/rubocop/cop/style/redundant_return.rb, line 92
def check_branch(node)
  return unless 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 :rescue, :resbody
    check_rescue_node(node)
  when :ensure then check_ensure_node(node)
  when :begin, :kwbegin
    check_begin_node(node)
  end
end
check_case_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 115
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_return.rb, line 133
def check_ensure_node(node)
  rescue_node = node.node_parts[0]
  check_branch(rescue_node)
end
check_if_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 120
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_return.rb, line 127
def check_rescue_node(node)
  node.child_nodes.each do |child_node|
    check_branch(child_node)
  end
end
check_return_node(node) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity

# File lib/rubocop/cop/style/redundant_return.rb, line 108
def check_return_node(node)
  return if cop_config['AllowMultipleReturnValues'] &&
            node.children.size > 1

  add_offense(node, location: :keyword)
end
message(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 151
def message(node)
  if !allow_multiple_return_values? && node.children.size > 1
    "#{MSG} #{MULTI_RETURN_MSG}"
  else
    MSG
  end
end