[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
This commit is contained in:
Tagir Valeev
2020-10-12 10:29:28 +00:00
committed by intellij-monorepo-bot
parent 3ca8478d9d
commit cde4e9cab2
3 changed files with 17 additions and 0 deletions
@@ -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);
}
@@ -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) { }
}
}
@@ -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(); }
}