nicklockwood / SwiftFormat

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

Swift 5.9 + disabling redundantReturn error #1565

Closed JOyo246 closed 8 months ago

JOyo246 commented 8 months ago

When bumping our swift version to 5.9, and also disabling redundantReturn, we are seeing a bug with if/switch expressions.

Swift 5.8 Code

enum MyEnum {
    case a
    case b
}
let myEnum = MyEnum.a
let test: Int = {
    switch myEnum {
    case .a:
        return 0
    case .b:
        return 1
    }
}()

After bumping to 5.9, disabling redundantReturn and running swiftformat, we get...


enum MyEnum {
    case a
    case b
}
let myEnum = MyEnum.a

let test: Int = switch myEnum {
    case .a:
        return 0
    case .b:
        return 1
}

which is an error, Cannot 'return' in 'switch' when used as expression

calda commented 8 months ago

Here's a fix: https://github.com/nicklockwood/SwiftFormat/pull/1567

nicklockwood commented 8 months ago

@calda oh ha ha, I was just finishing up my own fix 🤦‍♂️

I think I prefer my approach, but I'll steal your test suite - thanks!

calda commented 8 months ago

Haha, thats funny, np! Will be interested to see your approach

nicklockwood commented 8 months ago

@JOyo246 fixed in 0.52.9