menu

while-let-loop

Prefer while-let over an unconditional pattern loop

An unconditional loop that continues only while one pattern matches hides its condition in the body. Instead, you SHOULD place the successful pattern and value in a while-let condition.

The matched expression remains evaluated once at the start of each iteration.

Reported

1declare function next(): Result<int32, void>;2 3function consume(): void {4    loop {5        match (next()) {6            Ok { value } => {7                value;8            }9            Err { error: _ } => break10        }11    }12}

Accepted

1declare function next(): Result<int32, void>;2 3function consume(): void {4    while (let Ok { value } = next()) {5        value;6    }7}