Class: RuboCop::Cop::Sorbet::ForbidComparableTEnum

Inherits:
Base
  • Object
show all
Includes:
TEnum
Defined in:
lib/rubocop/cop/sorbet/t_enum/forbid_comparable_t_enum.rb

Overview

Disallow including the ‘Comparable` module in `T::Enum`.

Examples:


# bad
class Priority < T::Enum
  include Comparable

  enums do
    High = new(3)
    Medium = new(2)
    Low = new(1)
  end

  def <=>(other)
    serialize <=> other.serialize
  end
end

Constant Summary collapse

MSG =
"Do not use `T::Enum` as a comparable object because of significant performance overhead."
RESTRICT_ON_SEND =
[:include, :prepend].freeze

Instance Method Summary collapse

Methods included from TEnum

#after_class, #initialize, #on_class, #t_enum?

Instance Method Details

#mix_in_comparable?(node) ⇒ Object



32
33
34
# File 'lib/rubocop/cop/sorbet/t_enum/forbid_comparable_t_enum.rb', line 32

def_node_matcher :mix_in_comparable?, <<~PATTERN
  (send nil? {:include | :prepend} (const nil? :Comparable))
PATTERN

#on_send(node) ⇒ Object



36
37
38
39
40
# File 'lib/rubocop/cop/sorbet/t_enum/forbid_comparable_t_enum.rb', line 36

def on_send(node)
  return unless in_t_enum_class? && mix_in_comparable?(node)

  add_offense(node)
end