Submasks walked by hand
while s > 0: ..; s = (s - 1) & m: for s in submasks(m)
let n: 1..=16 = read()
let w: [0..(1 << n)]Int = read()
var best: List<Int> = [0 for _ in 0..(1 << n)]
for mask in 0..(1 << n):
var s = mask
while s > 0:
best[mask] max= w[s]
s = (s - 1) & mask
print(best)use std.math.{submasks}
let n: 1..=16 = read()
let w: [0..(1 << n)]Int = read()
var best = [0..(1 << n)] _ => 0
for mask in 0..(1 << n):
for s in submasks(mask):
if s > 0:
best[mask] max= w[s]
print(best)Why
s = (s - 1) & m steps s to the next smaller submask of m, so the loop var s = m; while s > 0: ..; s = (s - 1) & m visits every non-empty submask of m exactly once, largest first. Summed over all masks of n bits this is . for s in submasks(m) (std.math) is the same walk at the same cost, and says what it visits; it yields 0 last, which the hand loop stops before, so add if s > 0: where the body must not see the empty mask. Clearing the lowest bit, s = s & (s - 1), is a different walk (one bit at a time) and is not reported. There is no automatic fix, because whether the body may see 0 has to be read.
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.
let n: 1..=16 = read()
var bits = 0
var s = n
while s > 0:
bits += 1
s &= s - 1
print(bits)Turn it off
For one block, wrap it in with lint(submask_loop: allow):. For a project, add submask_loop = "allow" under [lints] in cp.toml.
Generated from spec/lints/submask_loop, the same record the linter is built from.