DFA: instanceof for unknown variable still may perform declared type check

Fixes IDEA-191374 DFA: "condition `x instanceof X` can be replaced with a null check" is not reported
This commit is contained in:
Tagir Valeev
2018-05-07 13:01:45 +07:00
parent 28b1de4b70
commit 24cde212c9
3 changed files with 20 additions and 1 deletions
@@ -817,7 +817,13 @@ public class DfaMemoryStateImpl implements DfaMemoryState {
}
if (dfaLeft instanceof DfaVariableValue) {
DfaVariableValue dfaVar = (DfaVariableValue)dfaLeft;
if (isUnknownState(dfaVar)) return true;
if (isUnknownState(dfaVar)) {
if (relationType == RelationType.IS_NOT) {
TypeConstraint constraint = factValue.get(DfaFactType.TYPE_CONSTRAINT);
return constraint == null || constraint.withNotInstanceofValue(dfaVar.getDfaType()) != null;
}
return true;
}
switch (relationType) {
case IS:
@@ -0,0 +1,12 @@
// IDEA-191374
class B {}
class A {
private B b = new B();
void foo() {
String s = b != null ? b.toString() : ""; //comment this line and the error will appear
if (<warning descr="Condition 'b instanceof B' is redundant and can be replaced with a null check">b instanceof B</warning>) { // no error reported here
System.out.println(b);
}
}
}
@@ -610,4 +610,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testMergedInitializerAndConstructor() { doTest(); }
public void testClassMethodsInlining() { doTest(); }
public void testObjectLocality() { doTest(); }
public void testInstanceOfForUnknownVariable() { doTest(); }
}