nicklockwood / SwiftFormat

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

`preferForLoop` doesn't deal well with `try` and `await` #1630

Closed SimplyDanny closed 4 months ago

SimplyDanny commented 4 months ago

The example

func list() throws -> [Int] { [1, 2, 3] }

try? list().forEach {
    print($0)
}

is rewritten to

func list() throws -> [Int] { [1, 2, 3] }

try? for item in list() {
    print(item)
}

which is invalid Swift code. There is probably no good solution for the try? case. try and try! could be moved in front of the list() call in the for loop just fine.

However, using try as in

try list().forEach {
    print($0)
}

the keyword will completely be removed by SwiftLint. The result is

for item in list() {
    print(item)
}

which is also incorrect and uncompilable.

There is the same issue for the await keyword.

nicklockwood commented 4 months ago

@SimplyDanny fixed in 0.53.3