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();