Read the smallest, then remove it
let x = s.first! then s.remove(x): s.pop_first()! (std.bag)
var s = SortedBag<Int>()
for x in [3, 1, 2]:
s.add(x)
while not s.is_empty:
let x = s.first!
s.remove(x)
print(x)var s = SortedBag<Int>()
for x in [3, 1, 2]:
s.add(x)
while let x = s.pop_first():
print(x)Why
let x = s.first! then s.remove(x) walks the tree twice, once to find the smallest and once to find it again by value. s.pop_first() (std.bag, on SortedBag and SortedSet) reads and removes it in one call and is None when s is empty; pop_last() is the same for the largest. The fix keeps the ! the original had.
The practice it serves
A hand-written pattern the language or the standard library already says in one step. Family: S.sorted: sets, bags, maps, intervals.
Not flagged
A near miss: close to the pattern, but not it.
var s = SortedBag<Int>()
for x in [3, 1, 2]:
s.add(x)
let x = s.first!
s.remove(x + 1)
let y = s.first!
print(x, y)Turn it off
For one block, wrap it in with lint(pop_min_loop: allow):. For a project, add pop_min_loop = "allow" under [lints] in cp.toml.
Generated from spec/lints/pop_min_loop, the same record the linter is built from.