From cde4e9cab29d193a9f999d977a790e8b157ea32b Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 12 Oct 2020 16:35:46 +0700 Subject: [PATCH] [java-dfa] Properly cast primitive type in getAdvancedExpressionDfaValue Fixes IDEA-252586 Wrong 'condition is always true' when element of double array is implicitly converted from int GitOrigin-RevId: 36cf865d5d9d88c14d2f3236a3d068555f7ca216 --- .../dataFlow/value/DfaExpressionFactory.java | 10 ++++++++++ .../inspection/dataFlow/fixture/DoubleArrayDiff.java | 6 ++++++ .../java/codeInspection/DataFlowInspectionTest.java | 1 + 3 files changed, 17 insertions(+) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/DoubleArrayDiff.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaExpressionFactory.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaExpressionFactory.java index c0e6a9dc37d6..7da80b23079d 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaExpressionFactory.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaExpressionFactory.java @@ -7,6 +7,7 @@ import com.intellij.codeInsight.Nullability; import com.intellij.codeInspection.dataFlow.*; import com.intellij.codeInspection.dataFlow.rangeSet.LongRangeSet; import com.intellij.codeInspection.dataFlow.types.DfConstantType; +import com.intellij.codeInspection.dataFlow.types.DfLongType; import com.intellij.codeInspection.dataFlow.types.DfType; import com.intellij.codeInspection.dataFlow.types.DfTypes; import com.intellij.psi.*; @@ -28,6 +29,8 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import static com.intellij.codeInspection.dataFlow.types.DfTypes.rangeClamped; + /** * @author peter */ @@ -235,6 +238,13 @@ public class DfaExpressionFactory { return myFactory.fromDfType(SpecialField.ARRAY_LENGTH.asDfType(DfTypes.intValue(length), type)); } DfType dfType = DfTypes.typedObject(type, NullabilityUtil.getExpressionNullability(expression)); + if (type instanceof PsiPrimitiveType && targetType instanceof PsiPrimitiveType && !type.equals(targetType)) { + if (TypeConversionUtil.isIntegralNumberType(targetType)) { + LongRangeSet range = DfLongType.extractRange(dfType); + return myFactory.fromDfType(rangeClamped(range.castTo((PsiPrimitiveType)targetType), PsiType.LONG.equals(targetType))); + } + return myFactory.fromDfType(DfTypes.typedObject(targetType, Nullability.UNKNOWN)); + } return DfaUtil.boxUnbox(myFactory.fromDfType(dfType), targetType); } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/DoubleArrayDiff.java b/java/java-tests/testData/inspection/dataFlow/fixture/DoubleArrayDiff.java new file mode 100644 index 000000000000..f1338133f1b1 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/DoubleArrayDiff.java @@ -0,0 +1,6 @@ +public class DoubleArrayDiff { + public static void test(int[] a, int[] b) { + double[] d1 = new double[]{b[0] - a[0], b[1] - a[1]}; + if (d1[1] != 0.0) { } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java index bf82ebc212a3..aac5c067b5f7 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java @@ -681,4 +681,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase { public void testBoxingShortByte() { doTest(); } public void testNullableAliasing() { doTest(); } public void testReapplyTypeArguments() { doTest(); } + public void testDoubleArrayDiff() { doTest(); } }