Skip to content

While-Guard-To-Condition

Sourcery rule id: while-guard-to-condition

Description

Move a guard clause in a while statement's body into its test

Before

while (true) {
    if (condition) {
        break;
    }
    do_some_stuff();
}

After

while (true && !condition) {
    do_some_stuff();
}

Explanation

A guard clause with a break that is the first thing in a while body is equivalent to having the condition contained in the while itself.