Credence.Pattern.NoTrivialDelegation
(credence v0.7.0)
Copy Markdown
Detects defp functions whose entire body is a direct call to a standard
library function with the same arguments passed through unchanged, and
inlines the standard library call at every call site, removing the wrapper.
These trivial wrappers add no value — the caller can use the standard library function directly.
Example
# Bad
defp string_length(str), do: String.length(str)
def run(s), do: string_length(s)
# Good
def run(s), do: String.length(s)Scope (the safe core)
The fix inlines the wrapper at every call site and deletes the definition. That is a whole-module transformation, so it only fires when the rewrite is provably behaviour-preserving for every input:
- the wrapper is a single private clause with that name (no other
defordefpof the same name, no extra clauses to dispatch on), - it has no guard (a guard would change which inputs are accepted),
- every argument is passed through unchanged, in order, to a well-known standard library function of the same arity,
- the wrapper is never captured (
&name/arity) and its name never appears as a bare atom (:name, e.g. insideapply/3) or as a variable, - every call site is an explicit call of the exact arity (no piped call with an elided argument), and there is at least one such call site.
Anything outside that core is deliberately left untouched: check/2 only
flags what fix_patches/2 can safely rewrite, so the two always agree.
The replacement is the wrapper's own body with the call-site arguments substituted in — so any module alias in scope (and the exact standard library function chosen) is reproduced verbatim, never re-derived.