class RuboCop::Cop::Style::ReturnNilInPredicateMethodDefinition
end
do_something?
return if condition
def foo?
# good
@example AllowedPatterns: [/foo/]
end
do_something?
return if condition
def foo?
# good
@example AllowedMethods: [‘foo?’]
end
do_something?
return false if condition
def foo?
# good
end
do_something?
return nil if condition
def foo?
# bad
end
do_something?
return if condition
def foo?
# bad
@example
from ‘nil` to `false` could potentially lead to incompatibility issues.
Autocorrection is marked as unsafe because the change of the return value
@safety
Checks if `return` or `return nil` is used in predicate method definitions.
def on_def(node)
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| next unless return_nil?(return_node) message = format(MSG, prefer: return_node.source) add_offense(return_node, message: message) do |corrector| corrector.replace(return_node, 'return false') end end end