Skip to content

Commit

Permalink
Fix handling of references to methods of array types and type variabl…
Browse files Browse the repository at this point in the history
…es (#926)

After #920 we check the nullability of the qualifier expression of a
method reference. We weren't correctly handling the cases where that
expression was an array type or a type variable
  • Loading branch information
msridhar authored Mar 4, 2024
1 parent d7c757f commit 348bb4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,7 @@ private boolean mayBeNullExpr(VisitorState state, ExpressionTree expr) {
return true;
case NEW_CLASS:
case NEW_ARRAY:
// for string concatenation, auto-boxing
case ARRAY_TYPE:
case LAMBDA_EXPRESSION:
// Lambdas may return null, but the lambda literal itself should not be null
case MEMBER_REFERENCE:
Expand Down Expand Up @@ -2485,7 +2485,8 @@ private Description matchDereference(
if (baseExpressionSymbol != null) {
if (baseExpressionSymbol.type.isPrimitive()
|| baseExpressionSymbol.getKind() == ElementKind.PACKAGE
|| ElementUtils.isTypeElement(baseExpressionSymbol)) {
|| ElementUtils.isTypeElement(baseExpressionSymbol)
|| baseExpressionSymbol.getKind() == ElementKind.TYPE_PARAMETER) {
// we know we don't have a null dereference here
return Description.NO_MATCH;
}
Expand Down
11 changes: 10 additions & 1 deletion nullaway/src/test/java/com/uber/nullaway/NullAwayJava8Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ public void methodReferenceOnNullableVariable() {
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"import java.util.Arrays;",
"import java.util.stream.Collectors;",
"class Test {",
" public static boolean test(@Nullable String text, java.util.Set<String> s) {",
" public static boolean testPositive(@Nullable String text, java.util.Set<String> s) {",
" // BUG: Diagnostic contains: dereferenced expression text is @Nullable",
" return s.stream().anyMatch(text::contains);",
" }",
" public static String[] testNegative(Object[] arr) {",
" // also tests we don't crash when the qualifier expression of a method reference is a type",
" return Arrays.stream(arr).map(Object::toString).toArray(String[]::new);",
" }",
" public static <T> boolean testNegativeWithTypeVariable(T[] arr) {",
" return Arrays.stream(arr).map(T::toString).collect(Collectors.toList()).isEmpty();",
" }",
"}")
.doTest();
}
Expand Down

0 comments on commit 348bb4d

Please sign in to comment.