From 2bb148e73e3ae3a7ebffa382a82532dec54b65bf Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Sat, 2 Dec 2023 17:46:28 +0000 Subject: [PATCH] Fix assertion failure in `redundantSelf` rule --- Sources/FormattingHelpers.swift | 2 +- Tests/RulesTests+Redundancy.swift | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Sources/FormattingHelpers.swift b/Sources/FormattingHelpers.swift index 4634e888..171f7924 100644 --- a/Sources/FormattingHelpers.swift +++ b/Sources/FormattingHelpers.swift @@ -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) { diff --git a/Tests/RulesTests+Redundancy.swift b/Tests/RulesTests+Redundancy.swift index ca70b9f1..aae79dd1 100644 --- a/Tests/RulesTests+Redundancy.swift +++ b/Tests/RulesTests+Redundancy.swift @@ -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