Class: RuboCop::Cop::Style::ReturnNil

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/style/return_nil.rb

Overview

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

This cop is disabled by default. Because there seems to be a perceived semantic difference between ‘return` and `return nil`. The former can be seen as just halting evaluation, while the latter might be used when the return value is of specific concern.

Supported styles are ‘return` and `return_nil`.

Examples:

EnforcedStyle: return (default)

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

# good
def foo(arg)
  return if arg
end

EnforcedStyle: return_nil

# bad
def foo(arg)
  return if arg
end

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

Constant Summary collapse

RETURN_MSG =
'Use `return` instead of `return nil`.'
RETURN_NIL_MSG =
'Use `return nil` instead of `return`.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_return(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/cop/style/return_nil.rb', line 48

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

#return_nil_node?(node) ⇒ Object



46
# File 'lib/rubocop/cop/style/return_nil.rb', line 46

def_node_matcher :return_nil_node?, '(return nil)'

#return_node?(node) ⇒ Object



43
# File 'lib/rubocop/cop/style/return_nil.rb', line 43

def_node_matcher :return_node?, '(return)'