class RuboCop::Cop::Style::RedundantReturn

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

@example AllowMultipleReturnValues: false (default)

# bad
def test
  return x, y
end

@example AllowMultipleReturnValues: true

# good
def test
  return x, y
end

Constants

MSG
MULTI_RETURN_MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 57
def on_def(node)
  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 92
def add_braces(corrector, node)
  corrector.insert_before(node.children.first, '{')
  corrector.insert_after(node.children.last, '}')
end
add_brackets(corrector, node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 87
def add_brackets(corrector, node)
  corrector.insert_before(node.children.first, '[')
  corrector.insert_after(node.children.last, ']')
end
allow_multiple_return_values?() click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 157
def allow_multiple_return_values?
  cop_config['AllowMultipleReturnValues'] || false
end
check_begin_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 152
def check_begin_node(node)
  last_expr = node.children.last
  check_branch(last_expr)
end
check_branch(node) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/rubocop/cop/style/redundant_return.rb, line 98
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 then check_rescue_node(node)
  when :resbody then check_resbody_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 126
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 147
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 131
def check_if_node(node)
  return if node.modifier_form? || node.ternary?

  check_branch(node.if_branch)
  check_branch(node.else_branch)
end
check_resbody_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 143
def check_resbody_node(node)
  check_branch(node.body)
end
check_rescue_node(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 138
def check_rescue_node(node)
  node.branches.each { |branch| check_branch(branch) }
  check_branch(node.body) unless node.else?
end
check_return_node(node) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity

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

  add_offense(node.loc.keyword, message: message(node)) do |corrector|
    if node.arguments?
      correct_with_arguments(node, corrector)
    else
      correct_without_arguments(node, corrector)
    end
  end
end
correct_with_arguments(return_node, corrector) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 68
def correct_with_arguments(return_node, corrector)
  if return_node.children.size > 1
    add_brackets(corrector, return_node)
  elsif hash_without_braces?(return_node.first_argument)
    add_braces(corrector, return_node.first_argument)
  end
  if return_node.splat_argument?
    first_argument = return_node.first_argument
    corrector.replace(first_argument, first_argument.source.delete_prefix('*'))
  end

  keyword = range_with_surrounding_space(return_node.loc.keyword, side: :right)
  corrector.remove(keyword)
end
correct_without_arguments(return_node, corrector) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 64
def correct_without_arguments(return_node, corrector)
  corrector.replace(return_node, 'nil')
end
hash_without_braces?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 83
def hash_without_braces?(node)
  node.hash_type? && !node.braces?
end
message(node) click to toggle source
# File lib/rubocop/cop/style/redundant_return.rb, line 161
def message(node)
  if !allow_multiple_return_values? && node.children.size > 1
    "#{MSG} #{MULTI_RETURN_MSG}"
  else
    MSG
  end
end