Sha256: 052842577f334b50666f3228ee89d09c9dc1e834421549de2aa984e87c7d3876

Contents?: true

Size: 1.08 KB

Versions: 33

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Enforces the use of `collection.exclude?(obj)`
      # over `!collection.include?(obj)`.
      #
      # @safety
      #   This cop is unsafe because false positive will occur for
      #   receiver objects that do not have an `exclude?` method. (e.g. `IPAddr`)
      #
      # @example
      #   # bad
      #   !array.include?(2)
      #   !hash.include?(:key)
      #
      #   # good
      #   array.exclude?(2)
      #   hash.exclude?(:key)
      #
      class NegateInclude < Base
        extend AutoCorrector

        MSG = 'Use `.exclude?` and remove the negation part.'
        RESTRICT_ON_SEND = %i[!].freeze

        def_node_matcher :negate_include_call?, <<~PATTERN
          (send (send $!nil? :include? $_) :!)
        PATTERN

        def on_send(node)
          return unless (receiver, obj = negate_include_call?(node))

          add_offense(node) do |corrector|
            corrector.replace(node, "#{receiver.source}.exclude?(#{obj.source})")
          end
        end
      end
    end
  end
end

Version data entries

33 entries across 32 versions & 6 rubygems

Version Path
rubocop-rails-2.29.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.29.0 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.28.0 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/negate_include.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/negate_include.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/negate_include.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/negate_include.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.21.2 lib/rubocop/cop/rails/negate_include.rb