diff --git a/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java b/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java index e44216745f..c0a0e02d50 100644 --- a/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java +++ b/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java @@ -308,6 +308,7 @@ public static Stream getTypeUseAnnotations( Symbol typeAnnotationOwner; switch (symbol.getKind()) { case PARAMETER: + // use the symbol's owner for parameters, unless it's the parameter of a lambda typeAnnotationOwner = symbol.owner; break; default: @@ -344,8 +345,16 @@ private static boolean targetTypeMatches(Symbol sym, TypeAnnotationPosition posi case PARAMETER: switch (position.type) { case METHOD_FORMAL_PARAMETER: - return ((Symbol.MethodSymbol) sym.owner).getParameters().indexOf(sym) - == position.parameter_index; + int parameterIndex = position.parameter_index; + if (position.onLambda != null) { + com.sun.tools.javac.util.List lambdaParams = + position.onLambda.params; + return parameterIndex < lambdaParams.size() + && lambdaParams.get(parameterIndex).sym.equals(sym); + } else { + return ((Symbol.MethodSymbol) sym.owner).getParameters().indexOf(sym) + == parameterIndex; + } default: return false; } diff --git a/nullaway/src/test/java/com/uber/nullaway/Java8Tests.java b/nullaway/src/test/java/com/uber/nullaway/Java8Tests.java index 478d3e1275..83b535f7ea 100644 --- a/nullaway/src/test/java/com/uber/nullaway/Java8Tests.java +++ b/nullaway/src/test/java/com/uber/nullaway/Java8Tests.java @@ -47,4 +47,24 @@ public void methodReferenceOnNullableVariable() { "}") .doTest(); } + + @Test + public void testNullableLambdaParamTypeUse() { + defaultCompilationHelper + .addSourceLines( + "Test.java", + "package com.uber;", + "import org.jspecify.annotations.Nullable;", + "class Test {", + " @FunctionalInterface", + " interface NullableParamFunctionTypeUse {", + " U takeVal(@org.jspecify.annotations.Nullable T x);", + " }", + " static void testParamTypeUse() {", + " NullableParamFunctionTypeUse n3 = (@org.jspecify.annotations.Nullable Object x) -> (x == null) ? \"null\" : x.toString();", + " NullableParamFunctionTypeUse n4 = (x) -> (x == null) ? \"null\" : x.toString();", + " }", + "}") + .doTest(); + } }