Credence.Pattern.PreferPatternMatchEmptyString
(credence v0.8.0)
Copy Markdown
Detects byte_size(var) == 0 in function guards that can be replaced with
pattern matching "" directly in the function head.
LLMs reach for byte_size/1 guards to check for empty strings because
many languages check .length == 0 or .size() == 0. In Elixir, pattern
matching "" in the function head is shorter, clearer, and more idiomatic.
Bad
def reverse_left_words(str, _count) when byte_size(str) == 0, do: str
def process(str) when byte_size(str) == 0, do: :emptyGood
def reverse_left_words("" = str, _count), do: str
def process(""), do: :emptyWhat is flagged
Any def/defp clause whose guard contains byte_size(param) == 0 where
param is a top-level simple parameter. The byte_size check may be the
sole guard or a direct conjunct in an and chain.
Not flagged:
byte_size(var) == 0insideorbyte_size(var) != 0(negated check)byte_size(expr) == 0where expr is not a simple variablebyte_size(var) == Nwhere N != 0
Auto-fix
Replaces the parameter with "" in the function head and removes
byte_size(param) == 0 from the guard (or drops the guard entirely if it
was the only condition). When the parameter is used in the function
body, the fix uses "" = param to preserve the binding.