Class: RuboCop::Cop::Naming::PredicateMethod
- 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).
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
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #on_def(node) ⇒ Object (also: #on_defs)
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
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
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 |