Credence.Pattern.NoListDeleteAtLength
(credence v0.7.0)
Copy Markdown
Detects List.delete_at(list, length(list) - 1) — computing the length
of a list just to index its last element for deletion.
Why this matters
length/1 is O(n) and List.delete_at/2 is another O(n), so the
combination walks the list twice. List.delete_at/2 already accepts
negative indexes counted from the end, so List.delete_at(list, -1)
deletes the last element in a single pass — the length call is pure
overhead.
Bad
middle = List.delete_at(tail, length(tail) - 1)Good
middle = List.delete_at(tail, -1)Detection scope
Matches List.delete_at(x, length(x) - 1) where x is the same
variable in both positions and the subtracted literal is exactly 1.
Larger offsets such as List.delete_at(x, length(x) - 2) are not
flagged: length(x) - K differs from -K for short lists (when
length(x) < K the left form yields a negative index that still
deletes an element from the end, while -K is out of range and deletes
nothing), so no constant-index rewrite is behaviour-preserving for them.