Unused Declaration
ID |
swift.unused_declaration |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Swift |
Tags |
dead-code, suspicious-code, unused-code |
Description
Reports a top-level private or fileprivate declaration — a class,
struct, enum, protocol, function, var or let — that is never
referenced anywhere else in the same file. Since the visibility scope
is the file itself, an absence of references in the file proves the
declaration is dead code that the developer most likely forgot to
delete.
private func deadHelper() { // FLAW — never called
print("unused")
}
private struct DeadBox { // FLAW — never instantiated
let value: Int = 0
}
private var unusedFlag = false // FLAW — never read
private func usedHelper() { ... } // OK — called below
usedHelper()
Rationale
Dead code accumulates as refactors land. A top-level private symbol
that no one in the file references serves no purpose: removing it does
not change behaviour, and keeping it forces every future reader to
re-prove that nothing reaches it. The rule limits itself to
file-local visibility (private, fileprivate) so that no
project-wide analysis is required — for higher visibilities the
declaration may be used by other files in the module or by other
modules, and a single-file rule cannot rule that out.
Remediation
Delete the unused declaration, or — if you actually meant to reach it
from another file — promote its visibility to internal or higher.
The following declarations are intentionally skipped:
-
@objc,@IBActionand@IBOutlet— reachable from Objective-C and Interface Builder, invisible to source-level analysis. -
Computed and observable properties.
-
Initializers.