Class: RuboCop::Cop::MagicNumbers::NoReturn
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::MagicNumbers::NoReturn
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'
- CONFIG_NAME_PERMITTED_RETURN_VALUES =
'PermittedReturnValues'
- RETURN_TYPE_IMPLICIT =
'Implicit'
- RETURN_TYPE_EXPLICIT =
'Explicit'
- RETURN_TYPE_NONE =
'None'
- DEFAULT_CONFIG =
{
CONFIG_NAME_ALLOWED_RETURNS => [RETURN_TYPE_NONE],
CONFIG_NAME_PERMITTED_RETURN_VALUES => []
}.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_config ⇒ Object
29
30
31
|
# File 'lib/rubocop/cop/magic_numbers/no_return.rb', line 29
def cop_config
DEFAULT_CONFIG.merge(super)
end
|
#on_method_defined(node) ⇒ Object
Also known as:
on_def
33
34
35
36
37
38
39
|
# File 'lib/rubocop/cop/magic_numbers/no_return.rb', line 33
def on_method_defined(node)
return if allowed_returns.include?(RETURN_TYPE_IMPLICIT)
return unless (captured_value = implicit_return?(node.children.last))
return if permitted_return_values.include?(captured_value)
add_offense(node.children.last, message: NO_EXPLICIT_RETURN_MSG)
end
|
#on_return(node) ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/rubocop/cop/magic_numbers/no_return.rb', line 42
def on_return(node)
return if allowed_returns.include?(RETURN_TYPE_EXPLICIT)
return unless forbidden_numerics.include?(node.children.first&.type)
return if permitted_return_values.include?(node.children.first&.value)
add_offense(node.children.first, message: NO_EXPLICIT_RETURN_MSG)
end
|