nicklockwood / SwiftFormat

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

`redundantParens` can make unexpected error #1671

Closed ski-u closed 7 months ago

ski-u commented 7 months ago

Hi, thank you for working hard at SwiftFormat! I tried a new version (v0.53.6) and found one unexpected behavior about redundantParens so I filed this issue ✏️

Reproduce

  1. Run swiftformat command to a fix file containing the following code
    
    import Foundation

func someOperation() async throws { let request = URLRequest(url: .init(string: "https://example.com")!) Task { async let dataTask1: Void = someTask(request) // Unexpected changes will occur here async let dataTask2: Void = someTask(request) } }

func someTask(_ request: URLRequest) async throws { URLSession.shared.dataTask(with: request) }


2. `swiftformat` make changes as following
```swift
import Foundation

func someOperation() async throws {
    let request = URLRequest(url: .init(string: "https://example.com")!)
    Task {
        async let dataTask1: Void = someTask request
        async let dataTask2: Void = someTask(request)
    }
}

func someTask(_ request: URLRequest) async throws {
    URLSession.shared.dataTask(with: request)
}

ℹ️ I used this command

❯ swiftformat . --verbose
Running SwiftFormat...
Formatting /Users/sakai.yunosuke/ghq/github.com/ski-u/MyPlayground.playground/Contents.swift
-- rules applied: redundantParens
Writing /Users/sakai.yunosuke/ghq/github.com/ski-u/MyPlayground.playground/Contents.swift
warning: No Swift version was specified, so some formatting features were disabled. Specify the version of Swift you are using with the --swiftversion option, or by adding a .swift-version file to your project.
SwiftFormat completed in 0.05s.
1/1 files formatted.

My environment

❯ swift --version
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
❯ xcodebuild -version
Xcode 15.3
Build version 15E204a
❯ swiftformat --version
0.53.6
wojciech-kulik commented 7 months ago

Same issue, shows errors for this line:

let wrapCompletion: (NetworkResponse<SuccessType, ErrorType>) -> () = { parameters in

and removes parentheses around NetworkResponse.

dumoko commented 7 months ago
      describe("getAlbums") {
        typealias PhotoEnumerationHandler = PhotoFetchResultEnumeration -> Void
        typealias PhotoCollectionEnumerationHandler = (PhotoCollection) -> Void

Removed the parens on the first typealias, but not the second. But if I extract the declaration of typealiases to the top of the file, swiftformat doesn't touch the parens.

nicklockwood commented 7 months ago

I think the common thread to all of these bugs is that the parens are the first ones inside a closure. I've pushed a fix to the develop branch.

nicklockwood commented 7 months ago

@ski-u fixed in 0.53.7

dumoko commented 7 months ago

@nicklockwood The issue still persists for this case:

      describe("getAlbums") {
        typealias PhotoCollectionEnumerationHandler = (PhotoCollection) -> Void
        typealias PhotoEnumerationHandler = (PhotoFetchResultEnumeration) -> Void
$ ./bin/swiftformat
Swiftformat version:  0.53.7
-- rules applied: redundantParens

SwiftFormat for Xcode would just remove the parens on the PhotoCollection causing a compile error:

Replace 'PhotoCollection' with '(PhotoCollection)'

Forced to use anonymous parameter workaround, but I would rather not :(

nicklockwood commented 7 months ago

@dumoko fixed in 0.53.8