class RuboCop::Cop::Style::ReturnNilInPredicateMethodDefinition
Checks for predicate method definitions that return ‘nil`. A predicate method should only return a boolean value.
@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 # bad def foo? if condition nil else true end end # good def foo? if condition false else true end 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
Source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 81 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) { |return_node| handle_return(return_node) } handle_implicit_return_values(body) end
Also aliased as: on_defs
Private Instance Methods
Source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 128 def handle_if(if_node) return unless if_node handle_implicit_return_values(if_node.if_branch) handle_implicit_return_values(if_node.else_branch) end
Source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 113 def handle_implicit_return_values(node) handle_if(last_node_of_type(node, :if)) handle_nil(last_node_of_type(node, :nil)) end
Source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 122 def handle_nil(nil_node) return unless nil_node register_offense(nil_node, 'false') end
Source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 118 def handle_return(return_node) register_offense(return_node, 'return false') if return_nil?(return_node) end
Source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 94 def last_node_of_type(node, type) return unless node return node if node_type?(node, type) return unless node.begin_type? return unless (last_child = node.children.last) last_child if last_child.is_a?(AST::Node) && node_type?(last_child, type) end
Source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 103 def node_type?(node, type) node.type == type.to_sym end
Source
# File lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb, line 107 def register_offense(offense_node, replacement) add_offense(offense_node) do |corrector| corrector.replace(offense_node, replacement) end end