Swift: Unused Parameters

New in v1.2 is a feature many Swift developers may miss from their Objective-C days: unused function parameter detection!

Along with the simple cases of unused parameters, Periphery also applies some heuristics to give you actionable results in more complex cases.
Take this example:

protocol Greeter {
    func greet(name: String)
    func farewell(name: String) // 'name' is unused
}

class InformalGreeter: Greeter {
    func greet(name: String) {
      print("Sup " + name + ".")
    }

    func farewell(name: String) { // 'name' is unused
      print("Cya.")
    }
}

class FormalGreeter: Greeter {
  func greet(name: String) {
    print("Nice to meet you.")
  }

  func farewell(name: String) { // 'name' is unused
    print("Goodbye.")
  }
}

Here we can see that name is unused in all implementations of farewell, however it’s only used in one implementation of greet. Since removing the parameter from greet is not possible, Periphery does not warn about the unused parameter in FormalGreeter.greet.

Similar logic also applies to overridden methods, parameters are only identified as unused if they’re also unused in the base function and all overriding functions. Another scenario in which unactionable results are silenced is for functions that simply call fatalError(). Such functions often have a valid reason for not being implemented, as is the case for unused required initializers in subclasses:

class Base {
    let param: String

    required init(param: String) {
        self.param = param
    }
}

class Subclass: Base {
    init(custom: String) {
        super.init(param: custom)
    }

    required init(param: String) { // 'param' is not reported as unused
        fatalError("init(param:) has not been implemented")
    }
}

A primary goal of Periphery is to only produce actionable results, hopefully these heuristics deliver on that. Though if you’re not in the mood for altering the signatures of your protocol functions, you can tell Periphery to ignore all unused parameters from protocols and conforming functions with the --retain-unused-protocol-func-params option.

The scan command applies all of the heuristics described here. However if you’re in a hurry, the new scan-syntax command will perform basic parameter analysis in a fraction of the time. Beware though, scan-syntax does not apply the protocol & override heuristics!

In case you missed the previous announcement, Periphery now provides 100% of results for free - including unused parameter results. Installation is quick and painless, so give it a try!