Product: SonarQube Community Build 26.5.0.122743
sonar-java version: sonar-java 8.29 (build 43460)
sonar-java SE version: sonar-java-symbolic-execution 8.16.4 (build 1912)
Java source level: 21 (javac 21, source/target 21)
Rule
S1751 - Loops with at most one iteration should be refactored
Description
There is an inconsistency in how rule java:S1751 handles for loops that execute at most once.
When a for loop has an explicit true condition and contains an unconditional break, the rule correctly raises an issue. However, when the condition is omitted for (;;) (which is semantically identical to for (;true;)), the rule fails to detect that the loop will also execute at most once (False Negative).
Both patterns represent a loop that runs at most one iteration and should be handled consistently.
Reproducer
public class LoopIterationInconsistency {
// Case A: S1751 fired (Correct)
void loopWithExplicitTrue() {
for (; true; ) {
break;
}
}
// Case B: NO finding (False Negative)
void loopWithEmptyCondition() {
for (; ; ) {
break;
}
}
}
Expected behavior
Both loopWithExplicitTrue and loopWithEmptyCondition should trigger a java:S1751 warning because both loops are guaranteed to terminate during the very first iteration due to the unconditional break statement.
Actual behavior
loopWithExplicitTruetriggersjava:S1751as expected.loopWithEmptyConditiondoes not trigger any issue. The static analysis engine seems to treatfor (;;)as a special syntax placeholder for infinite loops and consequently misses theS1751violation within its body.