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, @IBAction or @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 with test, …​) which are dispatched by the framework rather than called from user code.

Remediation

Delete the unused method. If it is intentionally part of an evolving API but kept in for a planned use, raise its visibility or annotate it with a // MARK: note explaining the intent, and add a call site:

class Service {
    private func used() {}
    func run() { used() }
}