← All lints

tree_order

L0511Idiomswarn

preorder or postorder of a rooted tree

t.preorder / t.postorder: iterate the preorder array t.at (t.at.rev for children first)

Flagged
domain V = 0..2
let t = Rooted(Graph.undirected([(V(0), V(1))]), V(0))
for v in t.postorder: print(v)
Write instead
domain V = 0..2
let t = Rooted(Graph.undirected([(V(0), V(1))]), V(0))
for v in t.at.rev: print(v)

Why

A Rooted tree has one traversal order, its preorder domain t.Pre (decision 63). for v in t.at visits each vertex before its descendants, for v in t.at.rev after them, and for (p, v) in t.at.items gives the position too. cpl lint --fix rewrites t.preorder to t.at and t.postorder to t.at.rev.

The practice it serves

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

Not flagged

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

Near miss
domain V = 0..2
let t = Rooted(Graph.undirected([(V(0), V(1))]), V(0))
print(t.size(V(0)))

Turn it off

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

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