Credence.Pattern.NoLengthComparisonForEmpty
(credence v0.4.0)
Copy Markdown
Detects length(list) comparisons with small integers (0–5) that can be
replaced with O(1) pattern matching.
length/1 is O(n) on linked lists — it traverses every element to count
them. LLMs use it freely because Python's len() is O(1). In Elixir,
pattern matching can answer the same questions in O(1).
Bad
length(list) == 0
length(list) > 0
length(list) < 2
length(list) >= 3Good
list == []
list != []
!match?([_, _ | _], list)
match?([_, _, _ | _], list)What is flagged
Any comparison of length(expr) with a literal integer 0–5 using
==, !=, >, >=, <, or <=. Reversed operands like
0 < length(list) are also detected. Comparisons with larger
integers are not flagged since the match pattern becomes unwieldy.
Auto-fix
Rewrites to == [], != [], match?/2, or !match?/2 depending
on the comparison. Only fixes when the argument to length/1 is a
simple variable name.