Credence.Pattern.NoCaseBooleanResult (credence v0.7.1)

Copy Markdown

Detects a two-clause case that converts a value to a boolean by matching a specific pattern against a trailing wildcard, and rewrites it to match?/2.

Note: this is the inverse of no_case_true_false, which catches case bool_expr do true -> …; false -> … end. This rule catches case expr do :ok -> true; _ -> false end.

Detected patterns (and their fixes)

case expr do :ok -> true; _ -> false end      match?(:ok, expr)
case expr do :ok -> false; _ -> true end      not match?(:ok, expr)

expr |> case do
  :ok -> true
  _ -> false
end                                            match?(:ok, expr)

The fired-on shape is exactly: two clauses, a specific (non-boolean, non-variable) pattern returning a boolean, then a wildcard (_) returning the opposite boolean. For such a case, match?/2 gives the identical answer on every input: the specific clause matches the same values, and the wildcard covers all the rest.

Deliberately not matched (no safe same-answer fix)

  • Two specific patterns (:ok -> true; :no -> false) — the case raises CaseClauseError on any other value, while a comparison returns false. Different answer, so not flagged.
  • Wildcard first (_ -> false; :ok -> true) — the wildcard matches everything, so the second clause is dead and the case is a constant. match?/2 is not constant, so not flagged.
  • A bare variable pattern (x -> true; _ -> false) — the variable matches everything, so the case is again constant. Not flagged.