Credence.Pattern.RedundantListGuard (credence v0.4.0)

Copy Markdown

Detects redundant is_list/1 guards on variables already bound by a cons pattern [head | tail]).

Why this matters

The pattern [head | tail] destructures a cons cell. While technically tail could be a non-list value (creating an improper list), in practice almost all Elixir code works with proper lists, making is_list(tail) guards on cons-tail variables redundant noise.

Flagged patterns

PatternFix
def f([h | t]) when is_list(t)def f([h | t])
def f([h | t]) when is_list(t) and is_atom(h)def f([h | t]) when is_atom(h)
def f([_ | a], [_ | b]) when is_list(a) and …Remove each redundant is_list call

Bad

def max_subarray_sum([first | rest]) when is_list(rest) do
  rest
end

def merge([h1 | t1], [h2 | t2]) when is_list(t1) and is_list(t2) do
  {t1, t2}
end

Good

def max_subarray_sum([first | rest]) do
  rest
end

def merge([h1 | t1], [h2 | t2]) do
  {t1, t2}
end