periphery icon indicating copy to clipboard operation
periphery copied to clipboard

False Positive: Inferred types used via `associatedtype` are incorrectly marked as unused

Open blindmonkey opened this issue 3 years ago • 0 comments

Using Xcode 13.3.1 or 13.4.1, Periphery reports a number of our types as unused. Upon looking into this more closely, I was able to reproduce it via something like the following:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Conformance()
            .padding()
    }
}

func getItems<P: TestProtocol>(_ value: P) -> [String] {
    value.result.items.map { "\($0.finish())" }
}

struct Conformance: View, TestProtocol {
    enum Output: String {
        case hello
        case world
    }
    let result = Result<Self>(items: [
        .init { .hello },
        .init { .world },
    ])
    var body: some View {
        Text(getItems(self).joined(separator: ", "))
    }
}

protocol TestProtocol {
    associatedtype Output

    var result: Result<Self> { get }
}

struct Result<P: TestProtocol> {
    struct Item {
        let finish: () -> P.Output
    }
    let items: [Item]
}

If ContentView is used properly, Periphery returns a warning for this code:

ContentView.swift:22:10: warning: Enum 'Output' is unused

Even though Output is used both when defining let result as well as a return type of finish() (which is also used), Periphery reports it as unused. Specifying Output explicitly (e.g. .init { Output.hello }) addresses the warning, but we'd rather avoid doing this in our code.

blindmonkey avatar Jul 27 '22 17:07 blindmonkey