Switch case formatting issue with SwiftLint
Asked Answered
I

3

5

My switch case is auto formatted when i do Cntrl + i , like below

switch someBool {
    ↓case true:
        print("success")
    ↓case false:
        print("failed")
}

but its throws a lint warning Switch and Case Statement Alignment Violation: Case statements should vertically align with their enclosing switch statement. (switch_case_alignment)

I have go manually format like below

switch someBool {
case true:
    print('red')
case false:
    print('blue')
}

but this changes as soon as i do Cntrl+I

Any suggestions are welcome. Thank you.

Isabeau answered 5/2, 2021 at 16:30 Comment(1)
What's your goal? To change Xcode's auto-formatting to match that particular SwiftLint setting? To make SwiftLint's suggestions match Xcode's auto-formatting? To make SwiftLint simply NOT CARE about the switch formatting? Or something else?Smitten
A
15

You can adjust that setting on Xcode with the following checkbox. I think it comes unchecked by default, which should match SwiftLint's default rules.

Xcode indentation settings

Airla answered 5/2, 2021 at 16:39 Comment(1)
Solution is Not Working.Tyrosine
A
1

For people running into this with expressions, this is a known issue with swiftlint.

// This triggers a violation:
let color: UIColor = switch state {
case [.highlighted]:
    .gray
case .selected, [.selected, .highlighted]:
    .white
case .disabled:
    .lightGray
default:
    .clear
}

The only workaround at time of writing is to disable the lint check as described in another answer.

Apologize answered 10/1 at 21:26 Comment(0)
O
0

When you get a SwiftLint Rule violation you can always go to this page for more information: https://realm.github.io/SwiftLint/switch_case_alignment.html

That shows you how to fix it. If you don't think you can easily fix it you can put an exemption in place to tell SwiftLint to ignore.

So you would just place this comment on the line above your switch statement:

// swiftlint:disable switch_case_alignment
Osis answered 5/2, 2022 at 1:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.