nicklockwood / SwiftFormat

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

Removing `return` in front of `try await` #1818

Open knutanderss opened 2 months ago

knutanderss commented 2 months ago

Hi, we had an instance in our codebase where SwiftFormat removed return as redundant in front of try await withCheckedThrowingContinuation { ... }, which caused a type error. The outer function returns Void, and withCheckedThrowingContinuation is generic on its return type, so without return the compiler is not able to infer that the generic parameter should be Void too.

It seems perhaps that return is not redundant in front of try await ...?

Anyway, disabling redundantReturn for that line works for now.

nicklockwood commented 2 months ago

@knutanderss I'm having a little bit of trouble visualizing the scenario you are talking about. Is it something like this?

func voidFunc() -> Void {
    return try await withCheckedThrowingContinuation { ... }
}

If not, perhaps you could provide a code example?

Smponias commented 3 weeks ago

@nicklockwood Some thing like this:

func abc() async throws {
    return try await withCheckedThrowingContinuation { continuation in
        DispatchQueue.main.async {
            someFunctionCall() { result, error in
                if let error {
                    continuation.resume(throwing: error)
                } else if let result {
                    continuation.resume()
                }
            }
        }
    }
}

If you remove the return you will get: Generic parameter 'T' could not be inferred