← All lints

manual_mex

L0548Idiomswarn

Hand-written first-unmarked search

while seen[m]: m += 1: (lo..=hi).find(m => not seen[m]) names the search

Flagged
let n: 1..=100 = read()
let a: [0..n]0..n = read()
var seen: [0..=n]Bool = false
for x in a:
    seen[x] = true
var m = 0
while seen[m]:
    m += 1
print(m)
Write instead
let n: 1..=100 = read()
let a: [0..n]0..n = read()
var seen: [0..=n]Bool = false
for x in a:
    seen[x] = true
print((0..=n).find(m => not seen[m])!)

Why

Walking a counter up while a table marks it (while seen[m]: m += 1) is a search for the first unmarked value: the mex, when the table marks the values present. (lo..=hi).find(m => not seen[m]) names the search and the range it runs over, so the end of the table is visible instead of implied. There is no automatic fix, because the range has to be read from the table's declaration.

The practice it serves

A hand-written pattern the language or the standard library already says in one step.

Not flagged

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

Near miss
let n: 1..=100 = read()
let a: [0..n]0..n = read()
var seen: [0..=n]Bool = false
for x in a:
    seen[x] = true
var m = 0
while seen[m]:
    m += 2
print(m)

Turn it off

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

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