Credence.Pattern.AvoidGraphemesEnumCount (credence v0.4.4)

Copy Markdown

Performance rule: Detects Enum.count/1 on the result of String.graphemes/1 (without a predicate).

Calling String.graphemes/1 eagerly allocates a list of every grapheme in the string. If the goal is simply to count characters, String.length/1 accomplishes this without the intermediate list.

Note: the predicate variant (Enum.count/2 with a filter function) is handled by the separate AvoidGraphemesEnumCountWithPredicate rule, which flags but does not auto-fix because the lazy-stream replacement can produce different results under Unicode normalization changes.

Bad

string |> String.graphemes() |> Enum.count()
Enum.count(String.graphemes(string))

Good

String.length(string)

# Or in a pipeline:
string |> String.length()