Sha256: b5ee4b01733e1978b65f9743e3955c405fa8e208ad3a5a0ec4ea9e7629a6c6f3

Contents?: true

Size: 778 Bytes

Versions: 4

Compression:

Stored size: 778 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

            convention(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.15.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-0.14.1 lib/rubocop/cop/style/nil_comparison.rb
rubocop-0.14.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-0.13.1 lib/rubocop/cop/style/nil_comparison.rb