Unused Private Function
ID |
swift.unused_private_function |
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 private or fileprivate method declared inside a type
(class, struct, enum, extension) whose name is never called anywhere
in the same source file. Because the visibility scope is at most the
file, the absence of a matching call site proves the method is dead
code that the developer most likely forgot to delete.
class Service {
private func helper() {} // FLAW — never called
private func used() {}
func run() { used() } // OK — referenced
}
Rationale
Forgotten private helpers accumulate, mislead the reader into thinking the routine still matters, and survive refactors that should have dropped them. Removing them keeps the file lean and surfaces the real public surface of the type.
The rule is intentionally conservative and skips:
-
methods annotated
@objc,@IBActionor@IBSegueAction— they are exposed to the Objective-C runtime or Interface Builder and may be invoked through channels invisible to source analysis; -
methods declared in a type with a protocol-conformance / inheritance clause — the helper may satisfy a protocol requirement the rule cannot see;
-
UIKit / AppKit / SwiftUI / XCTest lifecycle hooks (
viewDidLoad,applicationDidBecomeActive, names starting withtest, …) which are dispatched by the framework rather than called from user code.