Class: RuboCop::Cop::Rails::NegateInclude

Inherits:
RuboCop::Cop show all
Defined in:
lib/rubocop/cop/rails/negate_include.rb

Overview

This cop enforces the use of ‘collection.exclude?(obj)` over `!collection.include?(obj)`.

Examples:

# bad
!array.include?(2)
!hash.include?(:key)

# good
array.exclude?(2)
hash.exclude?(:key)

Constant Summary collapse

MSG =
'Use `.exclude?` and remove the negation part.'

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/rubocop/cop/rails/negate_include.rb', line 29

def autocorrect(node)
  negate_include_call?(node) do |receiver, obj|
    lambda do |corrector|
      corrector.replace(node, "#{receiver.source}.exclude?(#{obj.source})")
    end
  end
end

#on_send(node) ⇒ Object



25
26
27
# File 'lib/rubocop/cop/rails/negate_include.rb', line 25

def on_send(node)
  add_offense(node) if negate_include_call?(node)
end