Credence.Pattern.NoParamRebinding
(credence v0.4.3)
Copy Markdown
Style & correctness rule: Detects rebinding of parameter names inside
anonymous function (fn) bodies.
When a variable from the parameter destructure is rebound inside the body,
readers lose track of which binding is "live" at each point. This is a
common source of subtle bugs, especially in Enum.reduce callbacks where
the accumulator is destructured.
Bad
Enum.reduce(arr, {0, :queue.new()}, fn x, {count, q} ->
q = :queue.in(x, q) # rebinds `q` from the parameter
count = count + 1 # rebinds `count` from the parameter
{count, q}
end)Good
Enum.reduce(arr, {0, :queue.new()}, fn x, {count, q} ->
new_q = :queue.in(x, q)
new_count = count + 1
{new_count, new_q}
end)