Credence.Pattern.NoIsNilGuard (credence v0.5.0)

Copy Markdown

Detects is_nil(param) in function guards that can be replaced with pattern matching nil directly in the function head.

LLMs reach for is_nil/1 in guards because Python uses if x is None: — the explicit nil/null check is the only option. In Elixir, pattern matching nil in the function head is shorter, clearer, and more idiomatic.

Bad

def foo(x) when is_nil(x), do: :default
def bar(x, y) when is_nil(x) and is_binary(y), do: y

Good

def foo(nil), do: :default
def bar(nil, y) when is_binary(y), do: y

What is flagged

Any def/defp clause whose guard contains is_nil(param) where param is a top-level simple parameter. The is_nil may be the sole guard or a direct conjunct in an and chain.

Not flagged:

  • is_nil inside or (when is_nil(x) or is_atom(x))
  • negated is_nil (when not is_nil(x))
  • is_nil on non-variable expressions (when is_nil(hd(x)))
  • is_nil on destructured bindings (def foo(%{k: v}) when is_nil(v))
  • is_nil outside of function guards (e.g. inside if)

Auto-fix

Replaces the parameter with nil in the function head and removes is_nil(param) 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 nil = param to preserve the binding.