You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LoopConditionChecker reports a false positive when the increment operand is parenthesized #6213
Adding redundant parentheses around the operand of a loop's increment changes the result of LoopConditionChecker: i++ produces no finding, while (i)++ produces a false positive. In both cases, the loop's condition variable i is modified by the update. This manifests when the loop bound is a compile-time constant.
Per JLS §15.8.5, parentheses around an expression do not change its value, type, or constant-expression status. The two forms should therefore produce the same findings.
I verified the following pair on Error Prone 2.49.0:
Check
Rewrite
BEFORE
AFTER
Effect
LoopConditionChecker
i++ → (i)++
0
1
false positive
The matcher appears to inspect the increment operand without looking through ParenthesizedTree. A possible fix is to unwrap the operand before matching it, for example with ASTHelpers.stripParentheses.
Reproducers
LoopConditionChecker — (i)++ produces a false positive
(i)++ denotes the same variable update as i++, so the loop's condition variable i is still modified in the update. The check reports it as never modified (this manifests when the loop bound is a compile-time constant).
// BEFORE — no findingpublicclassLoopParen {
voidm() {
for (inti = 0; i < 10; i++) {
System.out.println(i);
}
}
}
// AFTER — false positive: "condition variable(s) never modified in loop body: i"publicclassLoopParen {
voidm() {
for (inti = 0; i < 10; (i)++) {
System.out.println(i);
}
}
}
Expected behavior
Neither version should produce a LoopConditionChecker finding. Both i++ and (i)++ modify the condition variable i, so adding redundant parentheses should not change the result.
Actual behavior
The BEFORE version produces 0 findings. The AFTER version produces 1 false positive: "condition variable(s) never modified in loop body: i", even though (i)++ modifies i.
Activity
Handle parenthesized expressions in `LoopConditionChecker`.
Automated rollback of commit a1750c3.