lib/rubocop/cop/style/non_nil_check.rb in rubocop-0.49.1 vs lib/rubocop/cop/style/non_nil_check.rb in rubocop-0.50.0
- old
+ new
@@ -22,12 +22,10 @@
# # good
# def signed_in?
# !current_user.nil?
# end
class NonNilCheck < Cop
- include OnMethodDef
-
def_node_matcher :not_equal_to_nil?, '(send _ :!= (:nil))'
def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
def_node_matcher :nil_check?, '(send _ :nil?)'
def_node_matcher :not_and_nil_check?, '(send (send _ :nil?) :!)'
@@ -36,14 +34,27 @@
if not_equal_to_nil?(node)
add_offense(node, :selector)
elsif include_semantic_changes? &&
(not_and_nil_check?(node) || unless_and_nil_check?(node))
- add_offense(node, :expression)
+ add_offense(node)
end
end
+ def on_def(node)
+ body = node.body
+
+ return unless node.predicate_method? && body
+
+ if body.begin_type?
+ ignore_node(body.children.last)
+ else
+ ignore_node(body)
+ end
+ end
+ alias on_defs on_def
+
private
def unless_and_nil_check?(send_node)
parent = send_node.parent
@@ -59,20 +70,9 @@
end
end
def include_semantic_changes?
cop_config['IncludeSemanticChanges']
- end
-
- def on_method_def(_node, name, _args, body)
- # only predicate methods are handled differently
- return unless name.to_s.end_with?('?') && body
-
- if body.begin_type?
- ignore_node(body.children.last)
- else
- ignore_node(body)
- end
end
def autocorrect(node)
case node.method_name
when :!=