From f17b330b6506f7010a85b3e395ab94977a20b3c8 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Fri, 20 Dec 2024 17:59:05 -0800 Subject: [PATCH] Support primitive static final fields as constant args in access paths (#1105) --- .../main/java/com/uber/nullaway/dataflow/AccessPath.java | 2 +- .../src/test/java/com/uber/nullaway/AccessPathsTests.java | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPath.java b/nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPath.java index a7b2d0a4e1..634548110e 100644 --- a/nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPath.java +++ b/nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPath.java @@ -742,7 +742,7 @@ private AccessPathContext(ImmutableSet immutableTypes) { } public boolean isStructurallyImmutableType(Type type) { - return immutableTypes.contains(type.tsym.toString()); + return type.isPrimitive() || immutableTypes.contains(type.tsym.toString()); } public static Builder builder() { diff --git a/nullaway/src/test/java/com/uber/nullaway/AccessPathsTests.java b/nullaway/src/test/java/com/uber/nullaway/AccessPathsTests.java index 354b00cf35..cdcfe46f33 100644 --- a/nullaway/src/test/java/com/uber/nullaway/AccessPathsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/AccessPathsTests.java @@ -79,12 +79,18 @@ public void testVariablesInAccessPathsNegative() { "import javax.annotation.Nullable;", "public class Test {", " private static final int INT_KEY = 42;", // Guaranteed constant! + " // Guaranteed constant after class loading", + " private static final int INT_KEY_HC = \"teststr\".hashCode();", " public void testEnhancedFor(NullableContainer> c) {", " if (c.get(\"KEY_STR\") != null && c.get(\"KEY_STR\").get(INT_KEY) != null) {", " c.get(\"KEY_STR\").get(INT_KEY).toString();", " c.get(\"KEY_STR\").get(Test.INT_KEY).toString();", " c.get(\"KEY_STR\").get(42).toString();", // Extra magic! " }", + " if (c.get(\"KEY_STR\") != null && c.get(\"KEY_STR\").get(INT_KEY_HC) != null) {", + " c.get(\"KEY_STR\").get(INT_KEY_HC).toString();", + " c.get(\"KEY_STR\").get(Test.INT_KEY_HC).toString();", + " }", " }", "}") .doTest();