# loop-single-iteration

Disallow loops whose control flow cannot reach another iteration

A loop whose body always exits or diverges cannot reach a second iteration and obscures its actual control flow.
Instead, you SHOULD use a conditional for optional execution or state an unconditional transfer directly.

- Category: suspicious
- Level: warning
- Fix: none
- Scope: module

## Reported

```ds title="main.ds"
declare function ready(): boolean;
declare function process(): void;

while (ready()) {
    process();
    break;
}
```

## Accepted

```ds title="main.ds"
declare function ready(): boolean;
declare function process(): void;

if (ready()) {
    process();
}
```

## Prior art

- [Clippy · never_loop](https://rust-lang.github.io/rust-clippy/master/index.html#never_loop)
- [ESLint · no-unreachable-loop](https://eslint.org/docs/latest/rules/no-unreachable-loop)
- [eslint-plugin-sonarjs · no-one-iteration-loop](https://github.com/SonarSource/SonarJS/search?q=no-one-iteration-loop&type=code)

[language/linter/src/rules/suspicious/loop_single_iteration.rs:6](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/suspicious/loop_single_iteration.rs#L6)
