Sha256: 7d1650acac189973b3999c9c69eb62ddd26a83a8f15b597d94695c262a5c86ad

Contents?: true

Size: 1.81 KB

Versions: 27

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for comparison of something with nil using `==` and
      # `nil?`.
      #
      # Supported styles are: predicate, comparison.
      #
      # @example EnforcedStyle: predicate (default)
      #
      #   # bad
      #   if x == nil
      #   end
      #
      #   # good
      #   if x.nil?
      #   end
      #
      # @example EnforcedStyle: comparison
      #
      #   # bad
      #   if x.nil?
      #   end
      #
      #   # good
      #   if x == nil
      #   end
      #
      class NilComparison < Base
        include ConfigurableEnforcedStyle
        extend AutoCorrector

        PREDICATE_MSG = 'Prefer the use of the `nil?` predicate.'
        EXPLICIT_MSG = 'Prefer the use of the `==` comparison.'

        RESTRICT_ON_SEND = %i[== === nil?].freeze

        def_node_matcher :nil_comparison?, '(send _ {:== :===} nil)'
        def_node_matcher :nil_check?, '(send _ :nil?)'

        def on_send(node)
          style_check?(node) do
            add_offense(node.loc.selector) do |corrector|
              new_code = if prefer_comparison?
                           node.source.sub('.nil?', ' == nil')
                         else
                           node.source.sub(/\s*={2,3}\s*nil/, '.nil?')
                         end

              corrector.replace(node, new_code)
            end
          end
        end

        private

        def message(_node)
          prefer_comparison? ? EXPLICIT_MSG : PREDICATE_MSG
        end

        def style_check?(node, &block)
          if prefer_comparison?
            nil_check?(node, &block)
          else
            nil_comparison?(node, &block)
          end
        end

        def prefer_comparison?
          style == :comparison
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nil_comparison.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nil_comparison.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nil_comparison.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nil_comparison.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nil_comparison.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.8.1 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.8.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.7.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.6.1 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.6.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.5.2 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.5.1 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.5.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.4.2 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.4.1 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.4.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.3.1 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.3.0 lib/rubocop/cop/style/nil_comparison.rb
rubocop-1.2.0 lib/rubocop/cop/style/nil_comparison.rb