From bde8d50fb51e41ab1e3440f6f94b1d63168f207a Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Thu, 6 Jan 2022 12:14:58 -0800 Subject: [PATCH] Add test case for unsound map reassignment handling (#541) It might be useful to have a suite of test cases showing where NullAway is unsound, for documentation purposes. Here I just add one for re-assigning a `Map` variable, but we could easily add more in the future. --- .../nullaway/NullAwayUnsoundnessTests.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 nullaway/src/test/java/com/uber/nullaway/NullAwayUnsoundnessTests.java diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayUnsoundnessTests.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayUnsoundnessTests.java new file mode 100644 index 0000000000..9078862cd6 --- /dev/null +++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayUnsoundnessTests.java @@ -0,0 +1,48 @@ +package com.uber.nullaway; + +import com.google.errorprone.CompilationTestHelper; +import java.util.Arrays; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** Unit tests showing cases where NullAway is unsound. Useful for documentation purposes. */ +public class NullAwayUnsoundnessTests { + + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private CompilationTestHelper defaultCompilationHelper; + + @Before + public void setup() { + defaultCompilationHelper = + CompilationTestHelper.newInstance(NullAway.class, getClass()) + .setArgs( + Arrays.asList( + "-d", + temporaryFolder.getRoot().getAbsolutePath(), + "-XepOpt:NullAway:AnnotatedPackages=com.uber")); + } + + @Test + public void mapReassignUnsound() { + defaultCompilationHelper + .addSourceLines( + "Test.java", + "package com.uber;", + "import java.util.*;", + "public class Test {", + " public void mapReassignUnsound(Map m, Object o) {", + " if (m.containsKey(o)) {", + " // NullAway is currently unsound for this case. It ignores", + " // the re-assignment of m and still assumes m.get(o) is non-null", + " // on the subsequent line.", + " m = new HashMap();", + " m.get(o).toString();", + " }", + " }", + "}") + .doTest(); + } +}