nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.92k stars 639 forks source link

Fix issue in #1392 where `opaqueGenericParamaters` would remove non-redundant generic type #1401

Closed calda closed 1 year ago

calda commented 1 year ago

This PR fixes #1392. In the following example, opaqueGenericParamaters was removing the T generic parameter because it appeared redundant / unused within the type definition. This behavior would have been correct if the constraint was something like where T == Int, but the constraint is actually important here since it's being constrained relative to a generic type from the parent scope.

public struct Ref<Value> {}

public extension Ref {
    static func weak<Base: AnyObject, T>(
        _: Base,
        _: ReferenceWritableKeyPath<Base, Value>
    ) -> Ref<Value> where T? == Value {}
}

As a fix we can just avoid removing parameters that seem unused. If the generic type was truly redundant / unused (e.g. with a T == Int constraint) the compiler would give us a generic parameter 'T' is not used in function signature error, so we don't have to worry about supporting that sort of case.

nicklockwood commented 1 year ago

Thank you!