Skip to content

Commit

Permalink
Suggest correct fix when array component of non-nullable array is mad…
Browse files Browse the repository at this point in the history
…e null. (#1087)
avenger2597 authored Dec 12, 2024
1 parent 6c498b6 commit 1fc39b3
Showing 2 changed files with 43 additions and 6 deletions.
7 changes: 1 addition & 6 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
@@ -502,13 +502,8 @@ public Description matchAssignment(AssignmentTree tree, VisitorState state) {
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(
errorMessage,
buildDescription(tree),
state,
ASTHelpers.getSymbol(tree.getVariable()));
errorMessage, buildDescription(tree), state, arraySymbol);
}
}
}
42 changes: 42 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/SerializationTest.java
Original file line number Diff line number Diff line change
@@ -2126,4 +2126,46 @@ public void anonymousSubClassConstructorSerializationTest() {
.setOutputFileNameAndHeader(ERROR_FILE_NAME, ERROR_FILE_HEADER)
.doTest();
}

@Test
public void errorSerializationTestArrayComponentNull() {
SerializationTestHelper<ErrorDisplay> tester = new SerializationTestHelper<>(root);
tester
.setArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
"-XepOpt:NullAway:SerializeFixMetadata=true",
"-XepOpt:NullAway:JSpecifyMode=true",
"-XepOpt:NullAway:FixSerializationConfigPath=" + configPath))
.addSourceLines(
"com/uber/A.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"public class A {",
" String [] foo = {\"SomeRandomWords\"};",
" void spin() {",
" // BUG: Diagnostic contains: Writing @Nullable expression into array with @NonNull contents.",
" foo[1] = null;",
" }",
"}")
.setExpectedOutputs(
new ErrorDisplay(
"ASSIGN_NULLABLE_TO_NONNULL_ARRAY",
"Writing @Nullable expression into array with @NonNull contents.",
"com.uber.A",
"spin()",
233,
"com/uber/A.java",
"FIELD",
"com.uber.A",
"null",
"foo",
"null",
"com/uber/A.java"))
.setFactory(errorDisplayFactory)
.setOutputFileNameAndHeader(ERROR_FILE_NAME, ERROR_FILE_HEADER)
.doTest();
}
}

0 comments on commit 1fc39b3

Please sign in to comment.