Class: RuboCop::Cop::Gusto::ObjectIn

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/gusto/object_in.rb

Overview

Identifies uses of ‘Object#in?`, which iterates over each item in a `Range` to see if a specified item is there. In contrast, `Range#cover?` simply compares the target item with the beginning and end points of the `Range`. In a great majority of cases, this is what is wanted.

This cop is unsafe. Here is an example of a case where ‘Range#cover?` may not provide the desired result:

('a'..'z').cover?('yellow') # => true

Constant Summary collapse

MSG =
'Use `Range#cover?` instead of `Object#in?`.'
RESTRICT_ON_SEND =
[:in?].freeze

Instance Method Summary collapse

Instance Method Details

#object_in(node) ⇒ Object



23
24
25
# File 'lib/rubocop/cop/gusto/object_in.rb', line 23

def_node_matcher :object_in, <<-PATTERN
  (call _ :in? {range (begin range)})
PATTERN

#on_send(node) ⇒ Object Also known as: on_csend



27
28
29
30
31
# File 'lib/rubocop/cop/gusto/object_in.rb', line 27

def on_send(node)
  return unless object_in(node)

  add_offense(node)
end