class RuboCop::Cop::Style::ReturnNilInPredicateMethodDefinition

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

@safety

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

@example

# 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

@example AllowedMethods: [‘foo?’]

# good
def foo?
  return if condition

  do_something?
end

@example AllowedPatterns: [/foo/]

# good
def foo?
  return if condition

  do_something?
end

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# 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
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

nil_node_at_the_end_of_method_body(body) click to toggle source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 79
def nil_node_at_the_end_of_method_body(body)
  return unless (last_child = body.children.last)

  last_child if last_child.is_a?(AST::Node) && last_child.nil_type?
end
register_offense(offense_node, replacement) click to toggle source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 85
def register_offense(offense_node, replacement)
  add_offense(offense_node) do |corrector|
    corrector.replace(offense_node, replacement)
  end
end