Skip to content

Explicitly typed lambda skips the @Nullable parameter check when the functional interface method is @NullUnmarked #1994

Closed

Description

@vlsi

An explicitly typed lambda can declare a non-null parameter where the functional interface method, declared @NullUnmarked, has an explicit @Nullable parameter, and NullAway reports nothing. An anonymous class, a method reference, and an implicitly typed lambda implementing the same method are all reported.

Version: master at 5ba2b54 (0.14.3-SNAPSHOT), JDK 21.0.9. Not tested on the 0.14.2 release.

Options:

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

The result is the same with -XepOpt:NullAway:OnlyNullMarked=true in place of AnnotatedPackages, and with -XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true in place of JSpecifyMode=true.

Reproducer (run as a CompilationTestHelper test extending NullAwayTestsBase):

package com.uber;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
  interface Fi {
    @NullUnmarked
    void apply(@Nullable Object o);
  }
  static void take(Object o) {}
  void test() {
    Fi anon = new Fi() {
      @Override
      public void apply(Object o) {}
    };
    Fi ref = Test::take;
    Fi implicit = o -> o.toString();
    Fi explicit = (Object o) -> o.toString();
  }
}

Actual: three warnings, none on line 19 (Fi explicit = (Object o) -> o.toString();):

/Test.java:15: warning: [NullAway] parameter o is @NonNull, but parameter in superclass method com.uber.Test.Fi.apply(java.lang.@org.jspecify.annotations.Nullable Object) is @Nullable
/Test.java:17: warning: [NullAway] parameter o of referenced method is @NonNull, but parameter in functional interface method com.uber.Test.Fi.apply(@Nullable Object) is @Nullable
/Test.java:18: warning: [NullAway] dereferenced expression 'o' is @Nullable

Expected: a fourth warning on line 19, parameter o is @NonNull, but parameter in functional interface method com.uber.Test.Fi.apply(@Nullable Object) is @Nullable, the message NullAway already gives for the same lambda when apply is not @NullUnmarked. JSpecify mode always acknowledges restrictive annotations in unmarked code (ErrorProneCLIFlagsConfig.acknowledgeRestrictiveAnnotations()), and the other three implementations on lines 15, 17, and 18 follow that.

Scope:

  • A named class class Impl implements Fi is reported like the anonymous class.
  • Nested nullness diverges the same way. With @NullUnmarked void apply(@Nullable Object @NonNull... inputs), @NullUnmarked void apply(@Nullable Object[] inputs), or @NullUnmarked void apply(List<@Nullable Object> l), an anonymous class whose parameter is Object[] or List<Object> gets Parameter has type Object [], but overridden method has parameter type @Nullable Object [], which has mismatched type parameter nullability (or the List equivalent), and the explicitly typed lambda gets nothing. The existing test JSpecifyVarargsTests#restrictiveAnnotationsVarargsLambdaUsesArrayNullness asserts no warning for the varargs lambda.
  • For nested nullness, explicitly typed lambdas are not checked even when the interface method is @NullMarked; open PR Check nested nullness for functional interface implementations #1739 adds that check inside checkParamOverriding, which the @NullUnmarked case never reaches.

Suspected location (a guess; the report stands without it): NullAway.matchLambdaExpression returns NO_MATCH when codeAnnotationInfo.isSymbolUnannotated(funcInterfaceMethod, ...) holds, before calling checkParamOverriding. That skips the onOverrideMethodInvocationParametersNullability handler call through which RestrictiveAnnotationHandler marks the @Nullable parameter for the anonymous class and the method reference.

Found by rewriting a lambda from NullAway's own test suite into an equivalent anonymous class.

Activity

  1. added 2 commits that reference this issue on Oct 21, 2026
    3688fa2
    29b740d
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions