Fix assertion failure in redundantSelf rule

This commit is contained in:
Nick Lockwood
2023-12-05 20:00:02 +00:00
parent f89e4e4620
commit 2bb148e73e
2 changed files with 23 additions and 2 deletions
+1 -1
View File
@@ -2753,7 +2753,7 @@ extension Formatter {
continue
case .startOfScope("{") where isWhereClause && scopeStack.count == 1:
return
case .startOfScope("{") where lastKeyword == "switch":
case .startOfScope("{") where lastKeyword == "switch" && scopeStack.count == 1:
lastKeyword = ""
index += 1
loop: while let token = self.token(at: index) {
+22 -1
View File
@@ -6505,7 +6505,28 @@ class RedundancyTests: RulesTests {
}
"""
let options = FormatOptions(swiftVersion: "5.9")
testFormatting(for: input, rule: FormatRules.indent, options: options)
testFormatting(for: input, rule: FormatRules.redundantSelf, options: options)
}
func testRedundantSelfNotConfusedByStaticAfterSwitch() {
let input = """
public final class MyClass {
private static func privateStaticFunction1() -> Bool {
switch Result(catching: { try someThrowingFunction() }) {
case .success:
return true
case .failure:
return false
}
}
private static func privateStaticFunction2() -> Bool {
return false
}
}
"""
let options = FormatOptions(explicitSelf: .insert)
testFormatting(for: input, rule: FormatRules.redundantSelf, options: options, exclude: ["enumNamespaces"])
}
// MARK: - redundantStaticSelf