Initializers declared as extension on Array are reported as unused
Original issue: https://github.com/peripheryapp/issues/issues/3 by ateliercw
Given a simplified example of:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = [String](title: "Title").first
}
}
extension Array where Element == String {
init(title: String) {
self = [title]
}
}
Periphery will report:
PeripheryTest/PeripheryTest/ViewController.swift:11:5: warning: Initializer 'init(title:)' is unused
This is causing a lot of false positives in a larger project that I'm working on do to expensive use of typed extensions on Array providing custom initializers.
@ateliercw This appears to be due to a SourceKit bug, it doesn't include any reference to the constructor when using an array literal. You can workaround it with Array(title: "Title").
I'll need to submit a bug report to Apple for this. Not that I expect them to ever fix it, all my previous reports have had zero response 😢
we've found probably the same problem in our project with Range:
import Foundation
class Bar {
func bar() -> Range<Double> {
return Range<Double>("foo")
}
}
private extension Range where Bound == Double {
init(_ string: String) {
self.init(uncheckedBounds: (lower: 1, upper: 2))
}
}
let bar = Bar()
print(bar.bar())
reports:
Periphery/main.swift:10:5: warning: Initializer 'init(_:)' is unused
Yep, I reported this issue in Feb 2018 here: https://bugs.swift.org/browse/SR-7093. Maybe it'll be fixed in Swift 6? 🤷♂️