Class: RuboCop::Cop::Style::ReturnNilInPredicateMethodDefinition

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
AllowedMethods, AllowedPattern
Defined in:
lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb

Overview

Checks if return or return nil is used in predicate method definitions.

Examples:

# bad
def foo?
  return if condition

  do_something?
end

# bad
def foo?
  return nil if condition

  do_something?
end

# good
def foo?
  return false if condition

  do_something?
end

AllowedMethods: ['foo?']

# good
def foo?
  return if condition

  do_something?
end

AllowedPatterns: [/foo/]

# good
def foo?
  return if condition

  do_something?
end

Cop Safety Information:

  • Autocorrection is marked as unsafe because the change of the return value from nil to false could potentially lead to incompatibility issues.

Constant Summary collapse

MSG =
'Return `false` instead of `nil` in predicate methods.'

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 inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, 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_def(node) ⇒ Object Also known as: on_defs



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb', line 62

def on_def(node)
  return unless node.predicate_method?
  return if allowed_method?(node.method_name) || matches_allowed_pattern?(node.method_name)
  return unless (body = node.body)

  body.each_descendant(:return) do |return_node|
    register_offense(return_node, 'return false') if return_nil?(return_node)
  end

  return unless (nil_node = nil_node_at_the_end_of_method_body(body))

  register_offense(nil_node, 'false')
end

#return_nil?(node) ⇒ Object



58
59
60
# File 'lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb', line 58

def_node_matcher :return_nil?, <<~PATTERN
  {(return) (return (nil))}
PATTERN