Swiftformat: Applying SwiftFormat to a `case let` binding breaks code

Created on 9 Oct 2020  路  7Comments  路  Source: nicklockwood/SwiftFormat

I've applied SwiftFormat to my Pods directory by accident. To my surprise this caused some code in it to break. One concrete example I've found is that this

guard case .some(Optional<Any>.some(let value)) = fieldValue else {

when reformatted to this

guard case let .some(Optional<Any>.some(value)) = fieldValue else {

stops compiling with '>' is not a postfix unary operator. This looks like a bug in Swift's toolchain parser to me, but until that's fixed it's probably best for SwiftFormat to work around that.

bug fixed in develop

All 7 comments

@MaxDesiatov thanks for reporting this. That's a weird one.

I've run into a little bit simpler case:

enum IntOrString {
    case int(Int)
    case string(String)
}

let value = IntOrString.string("Test")

guard case IntOrString
     .string(let innerValue) else { // it might be a common format when the type name is long and enum case has multiple associated values
          return
}

is formatted to:

guard case IntOrString
let .string(innerValue) = value else {
     return
}

and breaks the code. I used 0.47.1 version.

@marciniwanicki this is a different issue, and should be easier to fix.

Thanks @nicklockwood, do you mind if I create a new ticket for it?

https://github.com/nicklockwood/SwiftFormat/issues/786 for the case I mentioned earlier. Let me know if I can help with anything.

@MaxDesiatov fixed in 0.47.2

Great, thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Cyberbeni picture Cyberbeni  路  4Comments

zizi4n5 picture zizi4n5  路  3Comments

sebastianfrelle picture sebastianfrelle  路  4Comments

meherkasam picture meherkasam  路  3Comments

PompeiaPaetenari picture PompeiaPaetenari  路  3Comments