Sha256: 73a4b4d6ef1c394d1e90c204f2d0c6ee568d9b2c506f2ce2cf56e35cc478c4f4

Contents?: true

Size: 779 Bytes

Versions: 4

Compression:

Stored size: 779 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for comparison of something with nil using ==.
      #
      # @example
      #
      #  # bad
      #  if x == nil
      #  if x != nil
      #
      #  # good
      #  if x.nil?
      #  if !x.nil?
      class NilComparison < Cop
        MSG = 'Prefer the use of the nil? predicate.'

        OPS = %w(== === !=)

        NIL_NODE = s(:nil)

        def on_send(node)
          # lambda.() does not have a selector
          return unless node.loc.selector
          op = node.loc.selector.source

          if OPS.include?(op)
            _receiver, _method, args = *node

            add_offence(node, :selector) if args == NIL_NODE
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.18.1 lib/rubocop/cop/style/nil_comparison.rb
rubocop-0.18.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-0.17.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-0.16.0 lib/rubocop/cop/style/nil_comparison.rb