Credence.Pattern.NoRedundantNegatedGuard
(credence v0.4.3)
Copy Markdown
Detects guard clauses that are logically redundant because a preceding clause of the same function already handles the complementary case.
Why this matters
LLMs add "safety" guards because they don't trust Elixir's clause
ordering. When a clause with when a == b precedes one with
when a != b, the second guard is guaranteed to pass — anything
reaching that clause already failed the equality check:
# Flagged — second guard is redundant
defp compare([h1 | t1], [h2 | t2]) when h1 == h2, do: compare(t1, t2)
defp compare([h1 | _], [h2 |_ ]) when h1 != h2, do: h1
# Idiomatic — clause ordering handles it
defp compare([h1 | t1], [h1 | t2]), do: compare(t1, t2)
defp compare([missing | _],_ ), do: missingFix
The fix removes the redundant negated guard from the second clause. Since the preceding clause already matches the equality case, the negated guard adds no value — the clause ordering already ensures that only unequal pairs reach the second clause.
Flagged patterns
A function clause whose guard is when a != b or when a !== b,
immediately preceded by a clause of the same function/arity with
when a == b or when a === b (same variables, same positions).