nicklockwood / SwiftFormat

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

`redundantParens` removes required paranthesis for optional function calls. #1515

Closed lhunath closed 11 months ago

lhunath commented 1 year ago

Use case:

    public required init<V>(
        accessory: (() -> some View)? = nil as (() -> AnyView)?,
    ) {
        self.accessory = { (accessory?()).flatMap(AnyView.init(erasing:)) }
    }

In this scenario, the () around accessory?() is required. SwiftFormat's redundantParens rule removes them.

Without the parenthesis, the .flatMap operates on the result of the function (which is a non-optional View in this case), but instead it should operate on the optional result coming from the scenario where the function itself (the accessory closure) is nil.

$ swiftformat --version
0.52.2
$ cat .swift-version
5.8
$ swift --version
swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1)
Target: arm64-apple-macosx14.0
nicklockwood commented 11 months ago

@lhunath fixed in 0.52.3