Class: RuboCop::Cop::Homebrew::NegateInclude

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
rubocops/negate_include.rb

Overview

Note:

This cop is unsafe because false positive will occur for receiver objects that do not have an exclude? method. (e.g. IPAddr)

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 =
[:!].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
# File 'rubocops/negate_include.rb', line 33

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