Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive "method does not exist" error when using safe navigation #957

Open
dduugg opened this issue Jun 21, 2019 · 1 comment
Open
Labels
bug Something isn't working

Comments

@dduugg
Copy link
Contributor

dduugg commented Jun 21, 2019

Input

→ View on sorbet.run

class A
  extend T::Sig

  sig {
    params(x: T.nilable(String))
    .returns(T.any(FalseClass, NilClass, TrueClass))
  }
  def self.foo(x)
    x&.downcase&.end_with?(x.upcase)
  end
end

A.foo(nil)

Observed output

editor.rb:9: Method upcase does not exist on NilClass component of T.nilable(String) https://srb.help/7003
     9 |    x&.downcase&.end_with?(x.upcase)
                                   ^^^^^^^^
  Autocorrect: Use `-a` to autocorrect
    editor.rb:9: Replace with T.must(x)
     9 |    x&.downcase&.end_with?(x.upcase)
                                   ^
Errors: 1

Expected behavior

As x.upcase is only evaluated when x is non-nil, sorbet should not complain about the NilClass component of x at this point


(Discovered while applying Style/SafeNavigation autocorrection)

@dduugg dduugg added bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team labels Jun 21, 2019
@jez jez removed the unconfirmed This issue has not yet been confirmed by the Sorbet team label Jun 21, 2019
@jez jez changed the title False positive NilClass component error False positive "method does not exist" error when using safe navigation Jun 23, 2019
@SeekingMeaning

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants