Skip to content

Commit

Permalink
Check dereference of qualifier expression in method reference (#920)
Browse files Browse the repository at this point in the history
Fixes #919
  • Loading branch information
msridhar authored Feb 23, 2024
1 parent 3a3d8e7 commit a7bb7d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,14 @@ public Description matchMemberReference(MemberReferenceTree tree, VisitorState s
if (!withinAnnotatedCode(state)) {
return Description.NO_MATCH;
}
// Technically the qualifier expression of a method reference gets passed to
// Objects.requireNonNull, but it's fine to treat it as a dereference for error-checking
// purposes. The error message will be slightly inaccurate
Description derefErrorDescription =
matchDereference(tree.getQualifierExpression(), tree, state);
if (derefErrorDescription != Description.NO_MATCH) {
state.reportMatch(derefErrorDescription);
}
Symbol.MethodSymbol referencedMethod = ASTHelpers.getSymbol(tree);
Symbol.MethodSymbol funcInterfaceSymbol =
NullabilityUtil.getFunctionalInterfaceMethod(tree, state.getTypes());
Expand Down
16 changes: 16 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayJava8Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,20 @@ public void functionalMethodSuperInterface() {
public void functionalMethodOverrideSuperInterface() {
defaultCompilationHelper.addSourceFile("NullAwayOverrideFunctionalInterfaces.java").doTest();
}

@Test
public void methodReferenceOnNullableVariable() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" public static boolean test(@Nullable String text, java.util.Set<String> s) {",
" // BUG: Diagnostic contains: dereferenced expression text is @Nullable",
" return s.stream().anyMatch(text::contains);",
" }",
"}")
.doTest();
}
}

0 comments on commit a7bb7d3

Please sign in to comment.