Better fix for IDEA-245423 Incorrect "Condition X covered by subsequent condition Y" warning

Review ID: IDEA-CR-64866

GitOrigin-RevId: 7d2e5c40ce9d010975583f6fecc14ac6aff2a2e1
This commit is contained in:
Tagir Valeev
2020-07-15 04:13:55 +00:00
committed by intellij-monorepo-bot
parent 208f64d68d
commit 14f7907d29
3 changed files with 19 additions and 10 deletions
@@ -2,10 +2,7 @@
package com.intellij.codeInspection.dataFlow.value;
import com.intellij.codeInspection.dataFlow.DfaUtil;
import com.intellij.codeInspection.dataFlow.types.DfConstantType;
import com.intellij.codeInspection.dataFlow.types.DfIntegralType;
import com.intellij.codeInspection.dataFlow.types.DfType;
import com.intellij.codeInspection.dataFlow.types.DfTypes;
import com.intellij.codeInspection.dataFlow.types.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -106,12 +103,12 @@ public abstract class DfaCondition {
!DfaUtil.isNaN(((DfConstantType<?>)leftType).getValue()) ^
relationType == RelationType.EQ);
}
if (!rightType.isSuperType(leftType)) {
if (!couldBeEqualToConstant(rightType, leftType)) {
return fromBoolean(relationType == RelationType.NE);
}
}
else if (rightType instanceof DfConstantType) {
if (!leftType.isSuperType(rightType)) {
if (!couldBeEqualToConstant(leftType, rightType)) {
return fromBoolean(relationType == RelationType.NE);
}
}
@@ -141,5 +138,9 @@ public abstract class DfaCondition {
return null;
}
private static boolean couldBeEqualToConstant(DfType type, DfType constantType) {
return (type instanceof DfReferenceType ? ((DfReferenceType)type).dropTypeConstraint() : type).isSuperType(constantType);
}
}
}
@@ -179,10 +179,7 @@ public class DfaValueFactory {
return getNull();
}
if (variable instanceof PsiField && variable.hasModifierProperty(PsiModifier.STATIC) && ExpressionUtils.isNewObject(initializer)) {
PsiType initializerType = initializer.getType();
if (initializerType != null) {
return getConstant(variable, initializerType);
}
return getConstant(variable, type);
}
return null;
}
@@ -9,6 +9,17 @@ public class ConditionCoveredByFurtherCondition {
public void m() {
if (this == C1 || this == C2) { /* ...*/ }
}
public static final Object C3 = getC();
public static final Object C4 = getC();
public void m2() {
if (this == C3 || this == C4) {}
}
private static Object getC() {
return new C();
}
}
public void testInstanceOf(Object arg) {