Credence.Pattern.NoRedundantComparisonGuard (credence v0.7.0)

Copy Markdown

Detects redundant comparison guards in multi-clause functions.

When an earlier clause guards on type(var) and var < literal, a later clause's type(var) and var >= literal guard is always true for any value reaching it — the earlier clause already consumed the complementary domain. The same applies to >/<=, <=/>, >=/< pairs.

This extends the concept from NoRedundantNegatedGuard (which handles ==/!=) to comparison operators. Both clauses must carry the same type guard (e.g. is_number/1) so the comparison operates in a well-typed domain.

Why this matters

LLMs add "safety" comparison guards for the same reason they add negated guards — they don't trust Elixir's clause ordering. When clause 1 already handles n < 0, clause 3's n >= 0 is pure noise.

Flagged patterns

Earlier clauseLater clauseRedundant part
when is_number(n) and n < 0when is_number(n) and n >= 0n >= 0
when is_number(n) and n > 0when is_number(n) and n <= 0n <= 0
when is_integer(n) and n <= 5when is_integer(n) and n > 5n > 5

Not flagged (safe)

  • Bare comparisons without a shared type guard (non-numeric terms would break the complement logic).
  • Different type guards across clauses.
  • Different variables or literals.