Class: RuboCop::Cop::Sorbet::ImplicitConversionMethod

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sorbet/implicit_conversion_method.rb

Overview

Note:

Since the arity of aliased methods is not checked, false positives may result.

Disallows declaring implicit conversion methods. Since Sorbet is a nominal (not structural) type system, implicit conversion is currently unsupported.

Examples:


# bad
def to_str; end

# good
def to_str(x); end

# bad
def self.to_str; end

# good
def self.to_str(x); end

# bad
alias to_str to_s

See Also:

Constant Summary collapse

IMPLICIT_CONVERSION_METHODS =
[:to_ary, :to_int, :to_hash, :to_str].freeze
MSG =
"Avoid implicit conversion methods, as Sorbet does not support them. " \
"Explicity convert to the desired type instead."
RESTRICT_ON_SEND =
[:alias_method].freeze

Instance Method Summary collapse

Instance Method Details

#on_alias(node) ⇒ Object



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

def on_alias(node)
  new_id = node.new_identifier
  add_offense(new_id) if IMPLICIT_CONVERSION_METHODS.include?(new_id.value)
end

#on_def(node) ⇒ Object Also known as: on_defs



42
43
44
45
46
47
# File 'lib/rubocop/cop/sorbet/implicit_conversion_method.rb', line 42

def on_def(node)
  return unless IMPLICIT_CONVERSION_METHODS.include?(node.method_name)
  return unless node.arguments.empty?

  add_offense(node)
end

#on_send(node) ⇒ Object



50
51
52
# File 'lib/rubocop/cop/sorbet/implicit_conversion_method.rb', line 50

def on_send(node)
  add_offense(node.first_argument) if IMPLICIT_CONVERSION_METHODS.include?(node.first_argument.value)
end