← All lints

hand_rolled_range_tree

L0514Idiomswarn

Hand-rolled Fenwick or segment tree

manual BIT (p += p & -p) or segment-tree (2 * p / 2 * p + 1) loop: use std.fenwick / std.segtree / std.lazy

Flagged
var bit: [0..=8]Int = 0
var p = 3
while p <= 8:
    bit[p] += 1
    p += p & -p
print(bit[4])
Write instead
domain Pos = 1..=8
var bit = Fenwick<Pos, Int>()
bit.add(Pos(3), 1)
print(bit.prefix(Pos(4).r))

Why

Hand-rolling a binary indexed tree (p += p & -p / p -= p & -p) or a segment tree (2 * p and 2 * p + 1 with halving or midpoint recursion) duplicates tested, domain-safe stdlib range structures. Use Fenwick / DualFenwick / RangeFenwick or SegTree / LazySegTree / DualSegTree.

The practice it serves

A hand-written pattern the language or the standard library already says in one step. Family: S.range: segment trees, Fenwick, sparse tables.

Not flagged

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

Near miss
var p = 12
var steps = 0
while p > 0:
    p -= p & -p
    steps += 1
print(steps)

Turn it off

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

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