lib/rubocop/cop/style/non_nil_check.rb in rubocop-1.8.1 vs lib/rubocop/cop/style/non_nil_check.rb in rubocop-1.9.0

- old
+ new

@@ -6,10 +6,13 @@ # This cop checks for non-nil checks, which are usually redundant. # # With `IncludeSemanticChanges` set to `false` by default, this cop # does not report offenses for `!x.nil?` and does no changes that might # change behavior. + # Also `IncludeSemanticChanges` set to `false` with `EnforcedStyle: comparison` of + # `Style/NilComparison` cop, this cop does not report offenses for `x != nil` and + # does no changes to `!x.nil?` style. # # 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. # @@ -39,23 +42,27 @@ # end # class NonNilCheck < Base extend AutoCorrector + MSG_FOR_REPLACEMENT = 'Prefer `%<prefer>s` over `%<current>s`.' + MSG_FOR_REDUNDANCY = 'Explicit non-nil checks are usually redundant.' + RESTRICT_ON_SEND = %i[!= nil? !].freeze 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?) :!)' def on_send(node) - return if ignored_node?(node) - return unless (offense_node = find_offense_node(node)) + return if ignored_node?(node) || + !include_semantic_changes? && nil_comparison_style == 'comparison' + return unless register_offense?(node) message = message(node) - add_offense(offense_node, message: message) do |corrector| + add_offense(node, message: message) do |corrector| autocorrect(corrector, node) end end def on_def(node) @@ -71,17 +78,13 @@ end alias on_defs on_def private - def find_offense_node(node) - if not_equal_to_nil?(node) - node.loc.selector - elsif include_semantic_changes? && - (not_and_nil_check?(node) || unless_and_nil_check?(node)) - node - end + def register_offense?(node) + not_equal_to_nil?(node) || + include_semantic_changes? && (not_and_nil_check?(node) || unless_and_nil_check?(node)) end def autocorrect(corrector, node) case node.method_name when :!= @@ -99,14 +102,15 @@ nil_check?(send_node) && unless_check?(parent) && !parent.ternary? && parent.unless? end def message(node) - if node.method?(:!=) - 'Prefer `!expression.nil?` over `expression != nil`.' + if node.method?(:!=) && !include_semantic_changes? + prefer = "!#{node.receiver.source}.nil?" + format(MSG_FOR_REPLACEMENT, prefer: prefer, current: node.source) else - 'Explicit non-nil checks are usually redundant.' + MSG_FOR_REDUNDANCY end end def include_semantic_changes? cop_config['IncludeSemanticChanges'] @@ -135,9 +139,15 @@ end def autocorrect_unless_nil(corrector, node, receiver) corrector.replace(node.parent.loc.keyword, 'if') corrector.replace(node, receiver.source) + end + + def nil_comparison_style + nil_comparison_conf = config.for_cop('Style/NilComparison') + + nil_comparison_conf['Enabled'] && nil_comparison_conf['EnforcedStyle'] end end end end end