NullAway icon indicating copy to clipboard operation
NullAway copied to clipboard

Support optional.orElse(null)

Open garciat opened this issue 6 years ago • 4 comments

This will pass as non-null:

Optional.<Object>empty().orElse(null)

For example:

class Example {
  void f(Object o) {}

  void fails() {
    f(null);
  }

  void passesButShouldnt() {
    f(Optional.<Object>empty().orElse(null));
  }
}

I would expect anything.orElse( ... something Nullable ...) to be inferred to be Nullable.

garciat avatar Jul 13 '20 13:07 garciat

@lazaroclapp I think we could handle this with a @Contract annotation. Maybe we can do it as a library model?

msridhar avatar Jul 13 '20 21:07 msridhar

@lazaroclapp I think we could handle this with a @Contract annotation. Maybe we can do it as a library model?

Funny you should ask. I am about done with a PR that allows to encode the same semantics as @Contract(!null -> !null) as a library model ;)

lazaroclapp avatar Jul 13 '20 21:07 lazaroclapp

@Garciat This has now been fixed in master, we will try to cut a new version of NullAway with the fix (and the Error Prone upgrade) soon.

lazaroclapp avatar Jul 13 '20 22:07 lazaroclapp

Thanks! That was super quick 🙏

garciat avatar Jul 14 '20 07:07 garciat