Sha256: c90c259a86be58863acf99dd3472c316f9a62851adfb322a1a56fbad9382ff06

Contents?: true

Size: 689 Bytes

Versions: 1

Compression:

Stored size: 689 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)
          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

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.13.0 lib/rubocop/cop/style/nil_comparison.rb