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