Credence.Semantic.PreferKernelMaxOverLocal
(credence v0.8.0)
Copy Markdown
Removes a local defp max/2/defp min/2 that exactly re-implements the
auto-imported Kernel.max/2/Kernel.min/2.
LLMs frequently define a private max/2 with two guard clauses
(when a >= b / when b > a) even though Kernel.max/2 is auto-imported and
already provides this exact behaviour. The compiler then errors because the
local definition shadows the imported Kernel.max/2:
imported Kernel.max/2 conflicts with local functionWhy this is narrowed to the canonical body
The diagnostic fires for any local max/2 — including one that does
something completely different (defp max(a, b), do: a * b). Deleting that
body and redirecting callers to Kernel.max/2 would silently change the
answer. A behaviour-preserving fix is only possible when the local function is
provably the same function as the Kernel builtin.
So this rule only removes the local clauses when they are the exact canonical reimplementation:
defp max(a, b) when a >= b, do: a
defp max(a, b) when b > a, do: b(and the <=/< symmetric form for min). The >=/<= clause must return
the first parameter so the tie case agrees with Kernel for distinct-but-
equal terms (e.g. max(1, 1.0) returns 1, not 1.0). Anything else —
a different body, another arity sharing the name, a &max/2 capture, or a
piped x |> max(y) we can't safely requalify — is left untouched.