π Prefer putting the condition in the while statement.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
When a constant infinite loop immediately checks a condition and breaks, the loop condition is split across two places. Put the condition in the while statement so the loop's continuation condition is visible where readers expect it.
The rule only reports simple top-of-loop guards in while (true), for (;;), for (; true;), and do ... while (true) loops. Loops with labels, additional break statements targeting the same loop, or comments around the removable guard or loop syntax are ignored.
// β
while (true) {
if (!hasMore()) {
break;
}
processNext();
}// β
while (hasMore()) {
processNext();
}// β
do {
if (!hasMore()) {
break;
}
processNext();
} while (true);// β
while (hasMore()) {
processNext();
}// β
while (true) {
if (done) {
break;
}
processNext();
}// β
while (!done) {
processNext();
}// β
for (;;) {
if (!hasMore()) {
break;
}
processNext();
}// β
while (hasMore()) {
processNext();
}