From 1fc39b3b9c5496e980d65e169855b1fe19e27a66 Mon Sep 17 00:00:00 2001 From: Ankit Yadav <49056780+avenger2597@users.noreply.github.com> Date: Wed, 11 Dec 2024 18:31:57 -0800 Subject: [PATCH] Suggest correct fix when array component of non-nullable array is made null. (#1087) --- .../main/java/com/uber/nullaway/NullAway.java | 7 +--- .../com/uber/nullaway/SerializationTest.java | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/NullAway.java b/nullaway/src/main/java/com/uber/nullaway/NullAway.java index 38b1b1d6dd..6a31bb7bb6 100644 --- a/nullaway/src/main/java/com/uber/nullaway/NullAway.java +++ b/nullaway/src/main/java/com/uber/nullaway/NullAway.java @@ -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); } } } diff --git a/nullaway/src/test/java/com/uber/nullaway/SerializationTest.java b/nullaway/src/test/java/com/uber/nullaway/SerializationTest.java index a535ec4cbd..d828e9cf30 100644 --- a/nullaway/src/test/java/com/uber/nullaway/SerializationTest.java +++ b/nullaway/src/test/java/com/uber/nullaway/SerializationTest.java @@ -2126,4 +2126,46 @@ public void anonymousSubClassConstructorSerializationTest() { .setOutputFileNameAndHeader(ERROR_FILE_NAME, ERROR_FILE_HEADER) .doTest(); } + + @Test + public void errorSerializationTestArrayComponentNull() { + SerializationTestHelper 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(); + } }