Swiftformat: 'else' in multi-line 'guard' statement loses indentation

Created on 4 Aug 2020  路  4Comments  路  Source: nicklockwood/SwiftFormat

@nicklockwood I found some weird behaviour around multi-line guard statements. Here's my test example:

class MyClass {
    struct MyStruct {
        private func doSomething() {
            guard let foo = blah,
                bar.isSomething else {
                doSomething()
            }
        }
    }
}

Running the following command:
$ Pods/SwiftFormat/CommandLineTool/swiftformat test.swift --rules elseOnSameLine,wrapMultilineStatementBraces

Expected outcome:

class MyClass {
    struct MyStruct {
        private func doSomething() {
            guard let foo = blah,
                bar.isSomething
            else {
                doSomething()
            }
        }
    }
}

Actual outcome:

class MyClass {
    struct MyStruct {
        private func doSomething() {
            guard let foo = blah,
                bar.isSomething
else {
                doSomething()
            }
        }
    }
}
bug fixed in develop

All 4 comments

@meherkasam thanks for reporting, I'll get it fixed asap.

I think this was probably masked in testing by the fact the indent rule normally fixes it automatically.

Yup, you're right!

Running the following command works fine:
$ Pods/SwiftFormat/CommandLineTool/swiftformat test.swift --rules elseOnSameLine,wrapMultilineStatementBraces,indent

I didn't have the indent rule turned on yet.

@meherkasam fixed in 0.45.4.

Was this page helpful?
0 / 5 - 0 ratings