Credence.Pattern.PreferFunctionCapture
(credence v0.8.0)
Copy Markdown
Detects single-argument anonymous functions that simply delegate to a
function call and rewrites them using the more concise function capture
syntax (&Module.function/1 or &function/1).
Bad
Enum.map(list, fn x -> String.upcase(x) end)
Enum.map(list, fn x -> to_string(x) end)Good
Enum.map(list, &String.upcase/1)
Enum.map(list, &to_string/1)When NOT flagged
This rule only flags anonymous functions with exactly one parameter whose body is a single function call that uses the parameter exactly once as the sole argument. More complex patterns are not rewritten:
fn x -> x + 1 end # body is not a function call
fn x -> Enum.map(x, x) end # param used more than once
fn x, y -> Enum.map(x, y) end # more than one param
fn x -> Enum.map(x, &(&1 + 1)) end # extra arguments beyond the param