nicklockwood / SwiftFormat

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

hoistAwait misbehaves in async auto closures #1648

Closed alexito4 closed 3 months ago

alexito4 commented 3 months ago

When using autoclosures that have async expressions you need to use await inside the autoclosure. (Like Nimble async functions)

        let result = try await unwrapa(await asyncOptionalFunction())

Running swiftformat (0.53.4 and 0.53.5) with hoistAwait moves the inner await out

        let result = await try await unwrapa(asyncOptionalFunction())

causing a compiler error.

Screenshot 2024-03-19 at 14 11 32

Using // swiftformat:disable hoistAwait in the file indeeds avoid the issue.

nicklockwood commented 3 months ago

SwiftFormat cannot detect auto closure arguments automatically. You can fix this by manually specifying any async capturing functions in the SwiftFormat configuration with:

--asynccapturing unwrapa
alexito4 commented 3 months ago

You are right, I forgot about that option. Apologies, closing this ;)