Class: RuboCop::Cop::Naming::PredicateMethod

Inherits:
Base
  • Object
show all
Includes:
AllowedMethods, AllowedPattern
Defined in:
lib/rubocop/cop/naming/predicate_method.rb

Overview

Checks that predicate methods end with ‘?` and non-predicate methods do not.

The names of predicate methods (methods that return a boolean value) should end in a question mark. Methods that don’t return a boolean, shouldn’t end in a question mark.

The cop assesses a predicate method as one that returns boolean values. Likewise, a method that only returns literal values is assessed as non-predicate. Other predicate method calls are assumed to return boolean values. The cop does not make an assessment if the return type is unknown (non-predicate method calls, variables, etc.).

NOTE: The ‘initialize` method and operator methods (`def ==`, etc.) are ignored.

By default, the cop runs in ‘conservative` mode, which allows a method to be named with a question mark as long as at least one return value is boolean. In `aggressive` mode, methods with a question mark will register an offense if any known non-boolean return values are detected.

The cop also has ‘AllowedMethods` configuration in order to prevent the cop from registering an offense from a method name that does not confirm to the naming guidelines. By default, `call` is allowed. The cop also has `AllowedPatterns` configuration to allow method names by regular expression.

Although returning a call to another predicate method is treated as a boolean value, certain method names can be known to not return a boolean, despite ending in a ‘?` (for example, `Numeric#nonzero?` returns `self` or `nil`). These methods can be configured using `NonBooleanPredicates`.

The cop can furthermore be configured to allow all bang methods (method names ending with ‘!`), with `AllowBangMethods: true` (default false).

Examples:

Mode: conservative (default)

# bad
def foo
  bar == baz
end

# good
def foo?
  bar == baz
end

# bad
def foo?
  5
end

# good
def foo
  5
end

# bad
def foo
  x == y
end

# good
def foo?
  x == y
end

# bad
def foo
  !x
end

# good
def foo?
  !x
end

# bad - returns the value of another predicate method
def foo
  bar?
end

# good
def foo?
  bar?
end

# good - operator method
def ==(other)
  hash == other.hash
end

# good - at least one return value is boolean
def foo?
  return unless bar?
  true
end

# ok - return type is not known
def foo?
  bar
end

# ok - return type is not known
def foo
  bar?
end

Mode: aggressive

# bad - the method returns nil in some cases
def foo?
  return unless bar?
  true
end

AllowBangMethods: false (default)

# bad
def save!
  true
end

AllowBangMethods: true

# good
def save!
  true
end

Constant Summary collapse

MSG_PREDICATE =
'Predicate method names should end with `?`.'
MSG_NON_PREDICATE =
'Non-predicate method names should not end with `?`.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #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, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #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



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rubocop/cop/naming/predicate_method.rb', line 135

def on_def(node)
  return if allowed?(node)

  return_values = return_values(node.body)
  return if acceptable?(return_values)

  if node.predicate_method? && potential_non_predicate?(return_values)
    add_offense(node.loc.name, message: MSG_NON_PREDICATE)
  elsif !node.predicate_method? && all_return_values_boolean?(return_values)
    add_offense(node.loc.name, message: MSG_PREDICATE)
  end
end