Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check dereference of qualifier expression in method reference #920

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}