← All lints

stale_entry_skip

L0553Idiomswarn

Stale heap entry skipped by hand

if d > dist[u]: continue after a heap pop: dijkstra(g, from:)

Flagged
let (n, m): (1..=200000, 1..=200000) = read()
var adj: [1..=n]List<(Int, Int)> = []
for _ in 0..m:
    let (u, v, c): (1..=n, 1..=n, 1..=1000000000) = read()
    adj[u].push((v, c))
var dist: [1..=n]?Int = None
var pq = MinHeap<(Int, Int)>()
dist[1] = 0
pq.push((0, 1))
while let (d, u) = pq.pop():
    if d != dist[u]!:
        continue
    for (v, c) in adj[u]:
        if dist[v] == None or d + c < dist[v]!:
            dist[v] = d + c
            pq.push((d + c, v))
print(dist[n] ?? -1)
Write instead
let (n, m): (1..=200000, 1..=200000) = read()
domain V = 1..=n
domain E = 0..m
let es: [E](u: V, v: V, w: 1..=1000000000) = read()
let p = dijkstra(Graph.directed(es), from: [(V(1), 0)])
print(p[V(n)] ?? -1)

Why

C++ Dijkstra pushes a vertex again each time its distance improves and skips the old copies when they surface: if (d > dist[u]) continue;. The skip is the line people forget, and without it the loop is still correct but can run in O(m2)O(m^2). dijkstra(g, from: [(s, 0)]) from std.graph is the whole loop, over a Graph.directed(es) or Graph.undirected(es) built from typed edges with a w field: it settles each vertex once and returns Paths, where p[v] is the distance (None when unreachable) and p.path(v) the route. until: t stops once t is settled. The lint fires on if d > dist[u]: continue (or !=) after (d, u) is popped from a heap in a while. It has no fix: the graph has to be read as an edge list first.

The practice it serves

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

Not flagged

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

Near miss
let n: 1..=100 = read()
let limit: 0..=1000 = read()
let a: [0..n]Int = read()
var pq = MinHeap<(Int, Int)>()
for i in 0..n: pq.push((a[i], i))
var taken = 0
while let (d, i) = pq.pop():
    if d > limit:
        break
    taken += 1
print(taken)

Turn it off

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

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