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

JSpecify: Handle @Nullable assignments to @Nonnull arrays #929

Merged
merged 5 commits into from
Mar 11, 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
1 change: 1 addition & 0 deletions nullaway/src/main/java/com/uber/nullaway/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public enum MessageTypes {
PASS_NULLABLE_GENERIC,
WRONG_OVERRIDE_RETURN_GENERIC,
WRONG_OVERRIDE_PARAM_GENERIC,
ASSIGN_NULLABLE_TO_NONNULL_ARRAY,
}

public String getMessage() {
Expand Down
25 changes: 25 additions & 0 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static com.uber.nullaway.ASTHelpersBackports.isStatic;
import static com.uber.nullaway.ErrorBuilder.errMsgForInitializer;
import static com.uber.nullaway.NullabilityUtil.castToNonNull;
import static com.uber.nullaway.NullabilityUtil.isArrayElementNullable;

import com.google.auto.service.AutoService;
import com.google.auto.value.AutoValue;
Expand Down Expand Up @@ -497,6 +498,30 @@ public Description matchAssignment(AssignmentTree tree, VisitorState state) {
GenericsChecks.checkTypeParameterNullnessForAssignability(tree, this, state);
}

if (config.isJSpecifyMode() && tree.getVariable() instanceof ArrayAccessTree) {
// check for a write of a @Nullable value into @NonNull array contents
ArrayAccessTree arrayAccess = (ArrayAccessTree) tree.getVariable();
armughan11 marked this conversation as resolved.
Show resolved Hide resolved
ExpressionTree arrayExpr = arrayAccess.getExpression();
ExpressionTree expression = tree.getExpression();
Symbol arraySymbol = ASTHelpers.getSymbol(arrayExpr);
if (arraySymbol != null) {
boolean isElementNullable = isArrayElementNullable(arraySymbol, config);
if (!isElementNullable && mayBeNullExpr(state, expression)) {
final String message = "Writing @Nullable expression into array with @NonNull contents.";
ErrorMessage errorMessage =
new ErrorMessage(MessageTypes.ASSIGN_NULLABLE_TO_NONNULL_ARRAY, message);
// Future enhancements which auto-fix such warnings will require modification to this
// logic
return errorBuilder.createErrorDescription(
msridhar marked this conversation as resolved.
Show resolved Hide resolved
errorMessage,
expression,
buildDescription(tree),
state,
ASTHelpers.getSymbol(tree.getVariable()));
}
}
}

Symbol assigned = ASTHelpers.getSymbol(tree.getVariable());
if (assigned == null || assigned.getKind() != ElementKind.FIELD) {
// not a field of nullable type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,83 @@ public void arrayContentsAndTopLevelAnnotation() {
.doTest();
}

@Test
public void nullableAssignmentNonnullArray() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static String [] foo = new String[10];",
" static void foo() {",
" // BUG: Diagnostic contains: Writing @Nullable expression into array with @NonNull contents",
" foo[1] = null;",
" }",
"}")
.doTest();
}

@Test
public void nullableAssignmentNullableArray() {
msridhar marked this conversation as resolved.
Show resolved Hide resolved
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static @Nullable String [] foo = new String[10];",
" static void foo() {",
" // OK: since array elements are @Nullable",
" foo[1] = null;",
" }",
"}")
.doTest();
}

@Test
public void nullableAssignmentLocalArray() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static void foo() {",
" String [] nonNullArray = new String[10];",
" @Nullable String [] nullableArray = new String[10];",
" // BUG: Diagnostic contains: Writing @Nullable expression into array with @NonNull contents",
" nonNullArray[1] = null;",
" // OK: since array elements are @Nullable",
" nullableArray[1] = null;",
" }",
"}")
.doTest();
}

@Test
public void nullableAssignmentParameterArray() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static void fizz(String[] nonNullArray, @Nullable String[] nullableArray) {",
" // BUG: Diagnostic contains: Writing @Nullable expression into array with @NonNull contents",
" nonNullArray[1] = null;",
" // OK: since array elements are @Nullable",
" nullableArray[1] = null;",
" }",
" public static void main(String[] args) {",
" String[] foo = new String[10];",
" @Nullable String[] bar = new String[10];",
" fizz(foo, bar);",
" }",
"}")
.doTest();
}

private CompilationTestHelper makeHelper() {
return makeTestHelperWithArgs(
Arrays.asList(
Expand Down