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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/negate_include.rb

Overview

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.'
RESTRICT_ON_SEND =
%i[!].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/rubocop/cop/rails/negate_include.rb', line 32

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