Credence.Rule.NoEnumCountForLength
(credence v0.3.0)
Copy Markdown
Detects Enum.count/1 (without a predicate) and suggests length/1
or a more specific size function.
Why this matters
Enum.count/1 goes through the Enumerable protocol, adding dispatch
overhead. When the argument is a list — which it almost always is in
LLM-generated code — length/1 is a BIF that does the same traversal
without protocol dispatch:
# Flagged — protocol dispatch overhead
total = Enum.count(chars)
# Idiomatic — BIF, no protocol overhead
total = length(chars)For other collection types, more specific functions exist:
map_size/1 for maps, MapSet.size/1 for sets, tuple_size/1
for tuples.
Flagged patterns
Only the single-argument form Enum.count(x) is flagged.
The two-argument form Enum.count(x, predicate) is not flagged
because it filters and counts in one pass — there is no simpler
replacement.