Skip to content

Commit

Permalink
@nullable declaration annotation makes varargs array elements @nullable
Browse files Browse the repository at this point in the history
… in JSpecify mode
  • Loading branch information
msridhar committed Sep 1, 2024
1 parent bc46f2e commit 6b622c7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.TargetType;
import com.sun.tools.javac.code.Type;
Expand Down Expand Up @@ -432,6 +433,11 @@ public static boolean isArrayElementNullable(Symbol arraySymbol, Config config)
}
}
}
// In JSpecify mode, for varargs symbols we also consider the elements to be @Nullable if there
// is a @Nullable declaration annotation on the parameter
if (config.isJSpecifyMode() && (arraySymbol.flags() & Flags.VARARGS) != 0) {
return Nullness.hasNullableDeclarationAnnotation(arraySymbol, config);
}
return false;
}
}
3 changes: 2 additions & 1 deletion nullaway/src/main/java/com/uber/nullaway/Nullness.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ private static boolean individualVarargsParamsAreNullable(Symbol paramSymbol, Co
|| NullabilityUtil.isArrayElementNullable(paramSymbol, config);
}

private static boolean hasNullableDeclarationAnnotation(Symbol symbol, Config config) {
/** Checks if the symbol has a {@code @Nullable} declaration annotation */
public static boolean hasNullableDeclarationAnnotation(Symbol symbol, Config config) {
return hasNullableAnnotation(symbol.getRawAttributes().stream(), config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void testNullableVarargs() {
" public static String takesNullableVarargs(Object o, @Nullable Object... others) {",
" String s = o.toString() + \" \" + others.toString();",
" for (Object other : others) {",
" // no error here; requires a type-use annotation on the elements",
" // BUG: Diagnostic contains: dereferenced expression other is @Nullable",
" s += other.toString();",
" }",
" return s;",
Expand Down

0 comments on commit 6b622c7

Please sign in to comment.