lib/rubocop/cop/style/non_nil_check.rb in rubocop-0.75.1 vs lib/rubocop/cop/style/non_nil_check.rb in rubocop-0.76.0

- old
+ new

@@ -3,30 +3,42 @@ module RuboCop module Cop module Style # This cop checks for non-nil checks, which are usually redundant. # - # @example + # With `IncludeSemanticChanges` set to `false` by default, this cop + # does not report offenses for `!x.nil?` and does no changes that might + # change behavior. # + # With `IncludeSemanticChanges` set to `true`, this cop reports offenses + # for `!x.nil?` and autocorrects that and `x != nil` to solely `x`, which + # is **usually** OK, but might change behavior. + # + # @example # # bad # if x != nil # end # - # # good (when not allowing semantic changes) - # # bad (when allowing semantic changes) - # if !x.nil? - # end - # - # # good (when allowing semantic changes) + # # good # if x # end # - # Non-nil checks are allowed if they are the final nodes of predicate. - # + # # Non-nil checks are allowed if they are the final nodes of predicate. # # good # def signed_in? # !current_user.nil? # end + # + # @example IncludeSemanticChanges: false (default) + # # good + # if !x.nil? + # end + # + # @example IncludeSemanticChanges: true + # # bad + # if !x.nil? + # end + # class NonNilCheck < Cop 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?) :!)'