mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
+7
-1
@@ -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:
|
||||
|
||||
+12
@@ -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(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user