← All lints

chmin_chmax_if

L0523Idiomswarn

Hand-written min= / max=

best = max(best, x): the update is best max= x

Flagged
var best = 0
var low = 9
var total = 0
for v in [3, 1, 4]:
    total += v
    best = max(best, v)
    if v < low: low = v
print(best, low, total)
Write instead
var best = 0
var low = 9
var total = 0
for v in [3, 1, 4]:
    total += v
    best max= v
    low min= v
print(best, low, total)

Why

best = max(best, x) and if x > best: best = x write the place twice, and the copies must agree (best[i] = max(best[j], x) is a different statement that looks the same). CP Lang spells the update once: best max= x, lo min= x. The fix applies when nothing in the statement calls anything, so it evaluates exactly what the original did, and only on a place whose type is known and not optional.

The practice it serves

A hand-written pattern the language or the standard library already says in one step. Family: H. C++ habits.

Not flagged

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

Near miss
var best = 0
var low = 9
var total = 0
for v in [3, 1, 4]:
    total += v
    best = max(total, v)
    if v < low: low = total
print(best, low, total)

Turn it off

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

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