Credence.Pattern.NoGuardEqualityForPatternMatch (credence v0.8.0)

Copy Markdown

Readability rule: Detects guard clauses that compare a parameter to a literal value with == when pattern matching in the function head would be clearer and more idiomatic.

This only flags simple var == literal comparisons where var is one of the function's parameters and literal is an atom or string. Number literals are deliberately excluded: when n == 0 matches 0.0 (value equality) but the head f(0) does not (pattern uses ===), so substituting a number would change which clause a float-equal value routes to.

nil is an atom, so it is fixable too (when x == nilf(nil)): nil has no cross-type value-equal partner, so == nil and the nil head match the exact same inputs (notably NOT false).

Bad

defp do_count(n, _a, b) when n == 2, do: b
def process(action) when action == :stop, do: :halted
def encode(value, _) when value == nil, do: <<0>>

Good

defp do_count(2, _a, b), do: b
def process(:stop), do: :halted
def encode(nil, _), do: <<0>>