Skip to content

Missed nullable dereference in a lambda passed to a @NullUnmarked generic method #2205

Backlog

Description

@vlsi

Kind of problem

False negative (NullAway misses a nullness error)

Minimal reproducer

In @NullMarked code, a lambda passed to a @NullUnmarked generic method gets no error for dereferencing a nested @Nullable value. The same lambda passed to a @NullMarked method is reported.

package com.uber;

import java.util.function.Consumer;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class Repro {
  static class Box<E extends @Nullable Object> {
    E get() { throw new UnsupportedOperationException(); }
  }
  static <T> T marked(T value, Consumer<T> consumer) { return value; }
  @NullUnmarked static <T> T unmarked(T value, Consumer<T> consumer) { return value; }
  static <X> X outer(X value) { return value; }

  void markedStatement(Box<@Nullable String> box) {
    marked(box, p -> p.get().length());          // reported
  }
  void unmarkedStatement(Box<@Nullable String> box) {
    unmarked(box, p -> p.get().length());        // not reported
  }
  void unmarkedNested(Box<@Nullable String> box) {
    outer(unmarked(box, p -> p.get().length())); // not reported
  }
}

I compiled this file as a CompilationTestHelper test with the helper that GenericMethodTests uses (makeHelper()), with no expected-diagnostic markers, so the output below lists every error reported.

Actual behavior

/Repro.java:18: warning: [NullAway] dereferenced expression 'p.get()' is @Nullable
    marked(box, p -> p.get().length());          // reported
                            ^
    (see http://t.uber.com/nullaway )

No error is reported in unmarkedStatement or unmarkedNested.

Expected behavior

dereferenced expression 'p.get()' is @Nullable on p.get().length() in unmarkedStatement and in unmarkedNested, as in markedStatement. In each case T is inferred from the argument box of type Box<@Nullable String>. The lambda is in @NullMarked code, so its parameter p should have that type whether or not the callee is marked.

NullAway configuration

-XepOpt:NullAway:AnnotatedPackages=com.uber
-XepOpt:NullAway:JSpecifyMode=true
-XepOpt:NullAway:JSpecifyExperimental=true
-XDaddTypeAnnotationsToSymbol=true

NullAway version

master at af5ba08 (after 0.14.4)

Error Prone version

2.53.0

JDK version

OpenJDK 21.0.9

Reproduces with the latest NullAway release

No, not tried yet

Additional context

#2084 adds GenericsChecks.cacheInferredFunctionalArgumentTypesForUnmarkedCall (commit daeee58). In the review of that PR at its head d5d22a7, the nested form was reported and the statement form still was not: the hook sets the lambda's target only when inference for the call was already cached by another check, which does not happen when the call's result is unused. The review of that PR asks for the general fix to go into a separate change: #2084 (comment)

The closest existing issue is #1994, a missed @Nullable parameter check for an explicitly typed lambda whose functional interface method is @NullUnmarked. Here the functional interface is marked and the generic method that receives the lambda is unmarked.

Assisted-by: Claude Code (claude-opus-5-5)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions