DataFlowInstructionVisitor#isInstanceofRedundant fixed

GitOrigin-RevId: eacf2331a6aca6d00a5dbdb1a62d8249fab9ad1e
This commit is contained in:
Tagir Valeev
2019-11-29 13:17:58 +07:00
committed by intellij-monorepo-bot
parent a68034133d
commit ccb7c7addc
2 changed files with 12 additions and 4 deletions

View File

@@ -202,10 +202,14 @@ final class DataFlowInstructionVisitor extends StandardInstructionVisitor {
public boolean isInstanceofRedundant(InstanceofInstruction instruction) {
PsiExpression expression = instruction.getExpression();
return expression != null && !myUsefulInstanceofs.contains(instruction) && myReachable.contains(instruction) &&
myConstantExpressions.get(new ExpressionChunk(expression, null)) != ConstantResult.TRUE &&
(!(expression instanceof PsiMethodReferenceExpression) ||
!DfaConstValue.isConstant(myMethodReferenceResults.get(expression), Boolean.TRUE));
if (expression == null || myUsefulInstanceofs.contains(instruction) || !myReachable.contains(instruction)) return false;
if (expression instanceof PsiMethodReferenceExpression) {
DfaValue value = myMethodReferenceResults.get(expression);
return !(value instanceof DfaConstValue) || !(((DfaConstValue)value).getValue() instanceof Boolean);
} else {
ConstantResult result = myConstantExpressions.get(new ExpressionChunk(expression, null));
return result != ConstantResult.TRUE && result != ConstantResult.FALSE;
}
}
@Override

View File

@@ -11,4 +11,8 @@ class C {
System.out.println("Something");
}
}
void bar(A a) {
if (<warning descr="Condition 'a instanceof A' is redundant and can be replaced with a null check">a instanceof A</warning>) {}
else if (<warning descr="Condition 'a instanceof B' is always 'false'">a instanceof B</warning>) {}
}
}