← All lints

search_loop

L0501Idiomswarn

Flag-and-break search loop

a loop that sets a flag or ?T and breaks: first, any or all

Flagged
let xs: List<Int> = [1, 4, 7]
var found = false
for x in xs:
    if x > 3:
        found = true
        break
print(yes(found))
Write instead
let xs: List<Int> = [1, 4, 7]
let found = xs.any(x => x > 3)
print(yes(found))

Why

A loop that initializes a boolean flag or optional, updates it on a condition, and breaks can be written directly with any, all, or first.

The practice it serves

A hand-written pattern the language or the standard library already says in one step. Family: S.seq: reductions.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
let xs: List<Int> = [1, 4, 7]
var last = 0
for x in xs:
    if x > 3:
        last = x
print(last)

Turn it off

For one block, wrap it in with lint(search_loop: allow):. For a project, add search_loop = "allow" under [lints] in cp.toml.

Generated from spec/lints/search_loop, the same record the linter is built from.