Class: RuboCop::Cop::MagicNumbers::NoReturn

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/magic_numbers/no_return.rb

Overview

Raises an offense if a method returns with a magic number Catches both explicit and implicit returns

Constant Summary collapse

MAGIC_NUMBER_RETURN_PATTERN =
<<~PATTERN.chomp
  (%<illegal_scalar_pattern>s _)
PATTERN
NO_EXPLICIT_RETURN_MSG =
'Do not return magic numbers from a method or proc'
CONFIG_NAME_ALLOWED_RETURNS =
'AllowedReturns'
RETURN_TYPE_IMPLICIT =
'Implicit'
RETURN_TYPE_EXPLICIT =
'Explicit'
RETURN_TYPE_NONE =
'None'
DEFAULT_CONFIG =

Supported values are ‘Explicit’, ‘Implicit’, ‘None’

{
  CONFIG_NAME_ALLOWED_RETURNS => [RETURN_TYPE_NONE]
}.freeze

Constants inherited from Base

Base::CONFIG_ALL, Base::CONFIG_FLOAT, Base::CONFIG_INTEGER, Base::CONFIG_NAME_FORBIDDEN_NUMERICS, Base::ILLEGAL_SCALAR_TYPES

Instance Method Summary collapse

Instance Method Details

#cop_configObject



27
28
29
# File 'lib/rubocop/cop/magic_numbers/no_return.rb', line 27

def cop_config
  super.merge(DEFAULT_CONFIG)
end

#on_method_defined(node) ⇒ Object Also known as: on_def



31
32
33
34
35
36
# File 'lib/rubocop/cop/magic_numbers/no_return.rb', line 31

def on_method_defined(node)
  return if allowed_returns.include?(RETURN_TYPE_IMPLICIT)
  return unless implicit_return?(node.children.last)

  add_offense(node.children.last, message: NO_EXPLICIT_RETURN_MSG)
end

#on_return(node) ⇒ Object



39
40
41
42
43
44
# File 'lib/rubocop/cop/magic_numbers/no_return.rb', line 39

def on_return(node)
  return if allowed_returns.include?(RETURN_TYPE_EXPLICIT)
  return unless forbidden_numerics.include?(node.children.first&.type)

  add_offense(node.children.first, message: NO_EXPLICIT_RETURN_MSG)
end