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
LoopConditionChecker flags loops whose condition variable(s) are never modified in the loop body (a no-op or infinite loop). The check is sensitive to the syntactic form of the loop bound rather than its compile-time-constant value: replacing the literal 1 with a static final int ONE = 1 — which javac inlines as the constant 1 — silences the diagnostic, even though the loop is byte-for-byte identical at runtime and is still an infinite loop.
Reproducer
// BEFORE — LoopConditionChecker fires on the second loopclassC {
voidfoo() {
for (inti = 0; i < 1; i++) {} // i is modified -> not flaggedfor (inti = 0; i < 1; foo()) {} // i never modified -> FLAGGED
}
}
// AFTER — semantically identical (ONE is the constant 1), rule no longer firesclassC { staticfinalintONE = 1;
voidfoo() {
for (inti = 0; i < ONE; i++) {}
for (inti = 0; i < ONE; foo()) {} // still an infinite loop, NOT flagged
}
}
Expected behavior
LoopConditionChecker should report the same finding on BEFORE and AFTER. ONE is a compile-time constant equal to 1, so i < ONE and i < 1 are identical, and the second loop is an infinite loop in both forms.
Actual behavior
BEFORE: 1 finding — condition variable(s) never modified in loop body: i on the for (int i = 0; i < 1; foo()) loop.
AFTER: 0 findings. The checker bails out once the bound is a named constant instead of a literal, failing to constant-fold the condition operand.
Same on 2.50.0. The cause looks like LoopConditionVisitor.visitIdentifier: it only accepts a local, a parameter or a binding variable as a condition operand and returns false for any other identifier (LoopConditionChecker.java:110-122), so i < LIMIT with private static final int LIMIT = 10; is treated as a condition that might change. Treating an identifier with ASTHelpers.constValue(...) != null like a literal would report it.
LcSeed.java:
publicclassLcSeed {
intcount(int[] values) {
inttotal = 0;
for (inti = 0; i < 10; total++) { // [LoopConditionChecker]: i never modifiedtotal += values[total];
}
returntotal;
}
}
LcVariant.java:
publicclassLcVariant {
privatestaticfinalintLIMIT = 10;
intcount(int[] values) {
inttotal = 0;
for (inti = 0; i < LIMIT; total++) { // the same loop: not reportedtotal += values[total];
}
returntotal;
}
}
Command:
javac \
-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
-XDcompilePolicy=simple --should-stop=ifError=FLOW \
-processorpath error_prone_core-2.50.0-with-dependencies.jar -Xplugin:ErrorProne \
-d out LcSeed.java LcVariant.java
# the --add-exports/--add-opens flags are needed for Error Prone on a modern JDK;
# run the same command on each file
Expected:
[LoopConditionChecker] on the for in LcVariant.count (line 6), as on the one in LcSeed.count (line 4).
Output of the commands above:
### Error Prone 2.50.0 on javac 25.0.3 check: LoopConditionChecker
--- SEED (LcSeed.java) -- expected: [LoopConditionChecker]
LcSeed.java:4: error: [LoopConditionChecker] condition variable(s) never modified in loop body: i
for (int i = 0; i < 10; total++) { // [LoopConditionChecker]: i never modified
(see https://errorprone.info/bugpattern/LoopConditionChecker)
--- VARIANT (LcVariant.java) -- expected: [LoopConditionChecker], actual: not reported
(none)
Activity
Recognize compile-time constants in `LoopConditionChecker`.
MarkLee131 commentedon Nov 4, 2026
Same on 2.50.0. The cause looks like
LoopConditionVisitor.visitIdentifier: it only accepts a local, a parameter or a binding variable as a condition operand and returns false for any other identifier (LoopConditionChecker.java:110-122), soi < LIMITwithprivate static final int LIMIT = 10;is treated as a condition that might change. Treating an identifier withASTHelpers.constValue(...) != nulllike a literal would report it.LcSeed.java:LcVariant.java:Command:
Expected:
[LoopConditionChecker]on theforinLcVariant.count(line 6), as on the one inLcSeed.count(line 4).Output of the commands above: