sorbet/sorbet

Error context for "instance vs singleton" goes away in `--suggest-unsafe` mode

Offen

#8.338 geöffnet am 03.12.2024

 (0 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C++ (622 Forks)github user discovery
enhancementgood first issue

Repository-Metriken

Stars
 (3.791 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 10T 5h) (43 gemergte PRs in 30 T)

Beschreibung

Input

→ View on sorbet.run

# typed: true

class A
  def my_instance_method; end
  def self.my_singleton_class_method; end

  def example1
    my_singleton_class_method
  end
  def self.example2
    my_instance_method
  end
end

Observed output

editor.rb:8: Method my_singleton_class_method does not exist on A https://srb.help/7003
     8 |    my_singleton_class_method
            ^^^^^^^^^^^^^^^^^^^^^^^^^

editor.rb:11: Method my_instance_method does not exist on T.class_of(A) https://srb.help/7003
    11 |    my_instance_method
            ^^^^^^^^^^^^^^^^^^
Errors: 2

Expected behavior

Compare the output to what it looks like if you don't pass --suggest-unsafe:

→ View on sorbet.run

# typed: true

class A
  def my_instance_method; end
  def self.my_singleton_class_method; end

  def example1
    my_singleton_class_method
  end
  def self.example2
    my_instance_method
  end
end
editor.rb:8: Method my_singleton_class_method does not exist on A https://srb.help/7003
     8 |    my_singleton_class_method
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  There is a singleton class method with the same name:
    editor.rb:5: Defined here
     5 |  def self.my_singleton_class_method; end
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Either:
    - use .class to call it,
    - remove self. from its definition to make it an instance method, or
    - define the current method as a singleton class method using def self.
  Autocorrect: Use -a to autocorrect
    editor.rb:8: Insert self.class.
     8 |    my_singleton_class_method
            ^

editor.rb:11: Method my_instance_method does not exist on T.class_of(A) https://srb.help/7003
    11 |    my_instance_method
            ^^^^^^^^^^^^^^^^^^
  Did you mean instance_method? Use -a to autocorrect
    editor.rb:11: Replace with instance_method
    11 |    my_instance_method
            ^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/master/rbi/core/module.rbi#L1048: Defined here
    1048 |  def instance_method(arg0); end
            ^^^^^^^^^^^^^^^^^^^^^^^^^
Errors: 2

Contributor Guide