Skip to content

LoopConditionChecker fails to resolve compile-time constant loop bounds, missing genuine infinite loops #5826

Description

@Chordrain

Error Prone version

Error Prone 2.49.0 (javac plugin) — javac 21.0.8, source/target 21.

Check name

LoopConditionChecker

Description

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 loop
class C {
  void foo() {
    for (int i = 0; i < 1; i++) {}      // i is modified -> not flagged
    for (int i = 0; i < 1; foo()) {}    // i never modified -> FLAGGED
  }
}
// AFTER — semantically identical (ONE is the constant 1), rule no longer fires
class C { static final int ONE = 1;
  void foo() {
    for (int i = 0; i < ONE; i++) {}
    for (int i = 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.

Activity

  1. MarkLee131 commented on Nov 4, 2026

    @MarkLee131

    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:

    public class LcSeed {
        int count(int[] values) {
            int total = 0;
            for (int i = 0; i < 10; total++) {        // [LoopConditionChecker]: i never modified
                total += values[total];
            }
            return total;
        }
    }

    LcVariant.java:

    public class LcVariant {
        private static final int LIMIT = 10;
    
        int count(int[] values) {
            int total = 0;
            for (int i = 0; i < LIMIT; total++) {     // the same loop: not reported
                total += values[total];
            }
            return total;
        }
    }

    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)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions