Credence.Pattern.NoRedundantCaseNilClause (credence v0.7.0)

Copy Markdown

Detects case expressions where a nil clause and a trailing wildcard clause have identical bodies, and the intermediate guarded clause can be extended with not is_nil/1 to absorb the nil case.

In Erlang term ordering, atoms (including nil) sort above numbers, so nil >= 0 is true. LLMs frequently write an explicit nil -> guard before a var when var >= threshold -> clause to prevent nil from matching, then add a catch-all wildcard that duplicates the nil body. An expert would add not is_nil(var) to the guard and drop the redundant nil clause.

Detected patterns

case expr do
  nil -> body_a
  var when guard -> body_b
  _ -> body_a          # identical to nil body
end

Good

case expr do
  var when not is_nil(var) and guard -> body_b
  _ -> body_a
end

Auto-fix

Removes the nil clause and prepends not is_nil(var) and to the existing guard of the middle clause.