class RuboCop::Cop::Style::ReturnNil

Enforces consistency between ‘return nil’ and ‘return’.

Supported styles are: return, return_nil.

@example EnforcedStyle: return (default)

# bad
def foo(arg)
  return nil if arg
end

# good
def foo(arg)
  return if arg
end

@example EnforcedStyle: return_nil

# bad
def foo(arg)
  return if arg
end

# good
def foo(arg)
  return nil if arg
end

Constants

RETURN_MSG
RETURN_NIL_MSG

Public Instance Methods

on_return(node) click to toggle source
# File lib/rubocop/cop/style/return_nil.rb, line 44
def on_return(node)
  # Check Lint/NonLocalExitFromIterator first before this cop
  node.each_ancestor(:block, :def, :defs) do |n|
    break if scoped_node?(n)

    send_node, args_node, _body_node = *n

    # if a proc is passed to `Module#define_method` or
    # `Object#define_singleton_method`, `return` will not cause a
    # non-local exit error
    break if define_method?(send_node)

    next if args_node.children.empty?

    return nil if chained_send?(send_node)
  end

  return if correct_style?(node)

  add_offense(node) do |corrector|
    corrected = style == :return ? 'return' : 'return nil'

    corrector.replace(node, corrected)
  end
end

Private Instance Methods

correct_style?(node) click to toggle source
# File lib/rubocop/cop/style/return_nil.rb, line 76
def correct_style?(node)
  (style == :return && !return_nil_node?(node)) ||
    (style == :return_nil && !return_node?(node))
end
message(_node) click to toggle source
# File lib/rubocop/cop/style/return_nil.rb, line 72
def message(_node)
  style == :return ? RETURN_MSG : RETURN_NIL_MSG
end
scoped_node?(node) click to toggle source
# File lib/rubocop/cop/style/return_nil.rb, line 81
def scoped_node?(node)
  node.def_type? || node.defs_type? || node.lambda?
end