Unused Closure Parameter
ID |
swift.unused_closure_parameter |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Swift |
Tags |
code-smell, suspicious-code |
Description
Reports a named closure parameter whose name is never referenced from
inside the closure body. The closure would behave identically with the
parameter renamed to the wildcard _, which already signals
"intentionally ignored" and avoids misleading the reader into thinking
the binding matters.
[1, 2, 3].map { x, y in // FLAW on `y`
x * 2
}
[1, 2, 3].map { _, y in // OK — explicitly ignored
y * 2
}
Rationale
A named parameter is a contract with the reader: "this value is used
below." When the body never reads it, that contract is broken — the name
becomes noise that survives refactors and obscures what the closure
actually consumes. Swift offers _ as the conventional "this position
is required by the closure signature but is not consumed" marker, and
code review should require its use.
References from inside a nested closure that re-declares the same name are not counted — a parameter that is only "used" by a shadow binding is still reported as unused.
Remediation
Rename the unused parameter to _ to keep the closure signature
arity while making the intent explicit:
[1, 2, 3].map { _, y in
y * 2
}
[1, 2, 3].forEach { _ in
print("tick")
}