IDEA-188135 Report when always true/false expression in and/or chain causes short-circuit

This commit is contained in:
Tagir Valeev
2018-03-13 13:37:38 +07:00
parent f3900f45c6
commit d00541956f
2 changed files with 12 additions and 3 deletions
@@ -780,8 +780,14 @@ public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool
// Note that in `assert unknownExpression && trueExpression;` the trueExpression should not be reported
// because this assert is essentially the shortened `assert unknownExpression; assert trueExpression;`
// which is not reported.
anchor = parent;
continue;
boolean causesShortCircuit = (tokenType.equals(JavaTokenType.OROR) == evaluatesToTrue) &&
ArrayUtil.getLastElement(((PsiPolyadicExpression)parent).getOperands()) != anchor;
if (!causesShortCircuit) {
// We still report `assert trueExpression || unknownExpression`, because here `unknownExpression` is never checked
// which is probably not intended.
anchor = parent;
continue;
}
}
}
break;
@@ -23,7 +23,10 @@ class Test {
private static void testOr(boolean a, boolean b, boolean c) {
if(b) {
assert a || b || c;
// c is never checked: probably not intended; report
assert a || <warning descr="Condition 'b' is always 'true'">b</warning> || c;
assert a || c || b;
}
}