From f65c12ccbd8a39b90037cd4071cc27782ebe8bc4 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 7 Oct 2020 10:54:49 +0700 Subject: [PATCH] [java-dfa] Do not allow null in ephemeral value GitOrigin-RevId: cca32e434c96998346fc6759f3d699bc6ada799a --- .../dataFlow/types/DfGenericObjectType.java | 8 +++++--- .../dataFlow/fixture/EphemeralInIfChain.java | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/types/DfGenericObjectType.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/types/DfGenericObjectType.java index 534347174e8e..65e1e3e64ace 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/types/DfGenericObjectType.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/types/DfGenericObjectType.java @@ -249,9 +249,11 @@ class DfGenericObjectType extends DfAntiConstantType implements DfRefere } else if (!myNotValues.containsAll(otherNotValues)) { notValues = new THashSet<>(myNotValues); notValues.addAll(otherNotValues); - DfEphemeralReferenceType ephemeralValue = checkEphemeral(constraint, notValues); - if (ephemeralValue != null) { - return ephemeralValue; + if (nullability == DfaNullability.NOT_NULL) { + DfEphemeralReferenceType ephemeralValue = checkEphemeral(constraint, notValues); + if (ephemeralValue != null) { + return ephemeralValue; + } } } } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/EphemeralInIfChain.java b/java/java-tests/testData/inspection/dataFlow/fixture/EphemeralInIfChain.java index 38e606f9dcb8..a483a4808142 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/EphemeralInIfChain.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/EphemeralInIfChain.java @@ -14,10 +14,24 @@ class Test { else if (x == MyEnum.C) { s = "C"; } - System.out.println(s.trim()); + System.out.println(s.trim()); // reachable if x is null } - void test2(MyEnum x) { + void test1(@NotNull MyEnum x) { + String s = null; + if (x == MyEnum.A) { + s = "A"; + } + else if (x == MyEnum.B) { + s = "B"; + } + else if (x == MyEnum.C) { + s = "C"; + } + System.out.println(s.trim()); // ephemerably reachable + } + + void test2(@NotNull MyEnum x) { String s = null; if (x == MyEnum.A) { s = "A";