Skip to content

Commit 303e0e3

Browse files
committed
Fix library models check for expression-body lambdas
1 parent cb689ef commit 303e0e3

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

nullaway/src/main/java/com/uber/nullaway/NullAway.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,9 +1165,18 @@ public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState
11651165
updateEnvironmentMapping(state.getPath(), state);
11661166
handler.onMatchLambdaExpression(
11671167
tree, new MethodAnalysisContext(this, state, funcInterfaceMethod));
1168-
if (codeAnnotationInfo.isSymbolUnannotated(funcInterfaceMethod, config, handler)) {
1169-
return Description.NO_MATCH;
1168+
1169+
boolean isUnannotated = codeAnnotationInfo.isSymbolUnannotated(funcInterfaceMethod, config, handler);
1170+
if (isUnannotated) {
1171+
// Even if the functional interface is unannotated, a library model may override its
1172+
// return type to be @NonNull. If so, we should still check the lambda expression.
1173+
Nullness returnNullness =
1174+
getMethodReturnNullness(funcInterfaceMethod, state, Nullness.NULLABLE);
1175+
if (!returnNullness.equals(Nullness.NONNULL)) {
1176+
return Description.NO_MATCH;
1177+
}
11701178
}
1179+
11711180
Description description =
11721181
checkParamOverriding(
11731182
tree.getParameters().stream().map(ASTHelpers::getSymbol).collect(Collectors.toList()),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.uber.nullaway;
2+
3+
import org.junit.Test;
4+
5+
public class NullAwayLambdaTests extends NullAwayTestsBase {
6+
7+
@Test
8+
public void testLambdaExpressionLibraryModelOverride() {
9+
defaultCompilationHelper
10+
.addSourceLines(
11+
"Test.java",
12+
"package com.uber;",
13+
"import com.google.common.base.Function;",
14+
"public class Test {",
15+
" public void test() {",
16+
" // BUG: Diagnostic contains: returning @Nullable expression from method with @NonNull return type",
17+
" Function<Object, Object> f1 = (x) -> null;",
18+
" }",
19+
"}")
20+
.doTest();
21+
}
22+
}

0 commit comments

Comments
 (0)