IDEA-151355 Wrong 'condition always false' after using decrement on object field

This commit is contained in:
peter
2016-02-10 12:39:20 +01:00
parent c1fdc02b1a
commit f871d33fc8
3 changed files with 18 additions and 8 deletions
@@ -1667,14 +1667,11 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
}
private void flushIncrementedValue(@Nullable PsiExpression operand) {
if (operand instanceof PsiReferenceExpression && PsiUtil.isAccessedForWriting(operand)) {
PsiVariable psiVariable = DfaValueFactory.resolveUnqualifiedVariable((PsiReferenceExpression)operand);
if (psiVariable != null) {
DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(psiVariable, false);
addInstruction(new FlushVariableInstruction(dfaVariable));
if (psiVariable instanceof PsiField) {
addInstruction(new FlushVariableInstruction(null));
}
DfaValue dfaVariable = operand == null ? null : myFactory.createValue(operand);
if (dfaVariable instanceof DfaVariableValue && PsiUtil.isAccessedForWriting(operand)) {
addInstruction(new FlushVariableInstruction((DfaVariableValue)dfaVariable));
if (((DfaVariableValue)dfaVariable).getPsiVariable() instanceof PsiField) {
addInstruction(new FlushVariableInstruction(null));
}
}
}
@@ -0,0 +1,12 @@
class Test {
public class Node{
private int var;
}
public void foo(Node n){
assert(n.var>0);
n.var--;
if(n.var==0) { // condition always false
System.out.println();
}
}
}
@@ -238,6 +238,7 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testLongCircuitOperations() { doTest(); }
public void testUnconditionalForLoop() { doTest(); }
public void testIncrementParenthesized() { doTest(); }
public void testDecrementAnotherObjectField() { doTest(); }
public void testAnonymousMethodIndependence() { doTest(); }
public void testAnonymousFieldIndependence() { doTest(); }
public void testNoConfusionWithAnonymousConstantInitializer() { doTest(); }