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

Suggest correct fix when array component of non-nullable array is made null. #1087

Merged
merged 2 commits into from
Dec 12, 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
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
Expand Up @@ -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);
}
}
}
Expand Down
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
Expand Up @@ -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();
}
}
Loading