Class: RuboCop::Cop::Performance::AncestorsInclude

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/performance/ancestors_include.rb

Overview

Identifies usages of ‘ancestors.include?` and change them to use `<=` instead.

Examples:

# bad
A.ancestors.include?(B)

# good
A <= B

Constant Summary collapse

MSG =
'Use `<=` instead of `ancestors.include?`.'
RESTRICT_ON_SEND =
%i[include?].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/performance/ancestors_include.rb', line 30

def on_send(node)
  return unless (subclass, superclass = ancestors_include_candidate?(node))
  return if subclass && !subclass.const_type?

  add_offense(range(node)) do |corrector|
    subclass_source = subclass ? subclass.source : 'self'

    corrector.replace(node, "#{subclass_source} <= #{superclass.source}")
  end
end