Credence.Pattern.NoSplitToCount (credence v0.4.0)

Copy Markdown

Detects length(String.split(string, separator)) - 1 used to count substring occurrences.

This is a direct translation of Python's str.count(target). While it works correctly, it allocates the full list of split segments only to count them and throw the list away.

Bad

count = length(String.split(downcased, target)) - 1

Good

# For single-character targets, count matching graphemes directly:
count = downcased |> String.graphemes() |> Enum.count(&(&1 == target))

# For multi-character targets, use the Erlang binary matching BIF:
count = :binary.matches(downcased, target) |> length()

Auto-fix

Not auto-fixable — the best replacement depends on whether the separator is a single character or a multi-character substring.