NullAway icon indicating copy to clipboard operation
NullAway copied to clipboard

Lambda parameter confuses

Open agrieve opened this issue 1 month ago • 1 comments

import org.chromium.build.annotations.NullMarked;
import org.chromium.build.annotations.Nullable;

@NullMarked
class Foo {
  interface Func<ParamT extends @Nullable Object, RetT extends @Nullable Object> {
    RetT apply(ParamT p);
  }

  public static <RetT extends @Nullable Object> RetT run(Func<String, RetT> func) {
    return func.apply("");
  }

  static void repro() {
      run(s -> s == null ? null : s);
  }
}

give:

Foo.java:16: warning: [NullAway] returning @Nullable expression from method with @NonNull return type
      run(s -> s == null ? null : s);
          ^
    (see http://t.uber.com/nullaway )

The return type is <RetT extends @Nullable Object>, so null should be fine.

Interestingly, this lambda does not emit a warning:

      run(s -> null, "");

Using a subclass rather than a lambda also does not emit a warning.

agrieve avatar Dec 10 '25 03:12 agrieve

Thanks for the report. This doesn't work because we need to run a dataflow analysis within the lambda body to figure out that s == null ? null : s is in fact @Nullable. We haven't quite implemented that properly yet. FYI @dhruv-agr

msridhar avatar Dec 10 '25 04:12 msridhar