Skip to content

Commit

Permalink
Warn about unsupported @nullable var args case.
Browse files Browse the repository at this point in the history
  • Loading branch information
lazaroclapp committed Mar 29, 2019
1 parent 4da2895 commit 8251203
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions nullaway/src/main/java/com/uber/nullaway/ErrorMessage.java
Expand Up @@ -26,6 +26,7 @@ public enum MessageTypes {
FIELD_NO_INIT,
UNBOX_NULLABLE,
NONNULL_FIELD_READ_BEFORE_INIT,
NULLABLE_VARARGS_UNSUPPORTED,
ANNOTATION_VALUE_INVALID,
CAST_TO_NONNULL_ARG_NONNULL,
GET_ON_EMPTY_OPTIONAL;
Expand Down
21 changes: 18 additions & 3 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Expand Up @@ -484,10 +484,25 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
if (isOverriding || !exhaustiveOverride) {
Symbol.MethodSymbol closestOverriddenMethod =
getClosestOverriddenMethod(methodSymbol, state.getTypes());
if (closestOverriddenMethod == null) {
return Description.NO_MATCH;
if (closestOverriddenMethod != null) {
return checkOverriding(closestOverriddenMethod, methodSymbol, null, state);
}
}
// Check that var args (if any) is @Nullable, as NullAway doesn't currently support this case
if (methodSymbol.isVarArgs()) {
VarSymbol varArgsSymbol =
methodSymbol.getParameters().get(methodSymbol.getParameters().size() - 1);
if (Nullness.hasNullableAnnotation(varArgsSymbol)) {
String message =
"NullAway doesn't currently support @Nullable VarArgs. "
+ "Consider removing the @Nullable annotation from "
+ varArgsSymbol.toString()
+ " in "
+ methodSymbol.toString()
+ " (this issue can cause other errors below, wherever the var args array is dereferenced)";
return createErrorDescription(
MessageTypes.NULLABLE_VARARGS_UNSUPPORTED, tree, message, state.getPath());
}
return checkOverriding(closestOverriddenMethod, methodSymbol, null, state);
}
return Description.NO_MATCH;
}
Expand Down
21 changes: 21 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
Expand Up @@ -1707,6 +1707,27 @@ public void testNonNullVarargs() {
.doTest();
}

@Test
public void testNullableVarargs() {
compilationHelper
.addSourceLines(
"Utilities.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"public class Utilities {",
" // BUG: Diagnostic contains: NullAway doesn't currently support @Nullable VarArgs",
" public static String takesNullableVarargs(Object o, @Nullable Object... others) {",
" String s = o.toString() + \" \";",
" // BUG: Diagnostic contains: enhanced-for expression others is @Nullable",
" for (Object other : others) {",
" s += (other == null) ? \"(null) \" : other.toString() + \" \";",
" }",
" return s;",
" }",
"}")
.doTest();
}

@Test
public void testNonNullVarargsFromHandler() {
String generatedAnnot =
Expand Down

0 comments on commit 8251203

Please sign in to comment.