periphery icon indicating copy to clipboard operation
periphery copied to clipboard

Initializers declared as extension on Array are reported as unused

Open ileitch opened this issue 7 years ago • 3 comments

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.

ileitch avatar Feb 10 '19 18:02 ileitch

@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 😢

ileitch avatar Feb 19 '19 17:02 ileitch

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

aisohaikens avatar May 10 '21 19:05 aisohaikens

Yep, I reported this issue in Feb 2018 here: https://bugs.swift.org/browse/SR-7093. Maybe it'll be fixed in Swift 6? 🤷‍♂️

ileitch avatar May 11 '21 08:05 ileitch