diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 1fa9fda2..fd304ed3 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3137,10 +3137,11 @@ public struct _FormatRules { return } } - if formatter.index(of: .keyword("return"), after: i) != nil { + let endIndex = formatter.endOfScope(at: i) + if let endIndex = endIndex, formatter.tokens[i + 1 ..< endIndex].contains(.keyword("return")) { return } - if formatter.next(.nonSpaceOrLinebreak, after: i) == .endOfScope("}"), + if formatter.index(of: .nonSpaceOrLinebreak, after: i) == endIndex, let startIndex = formatter.index(of: .nonSpaceOrLinebreak, before: i) { formatter.removeTokens(in: startIndex + 1 ... i) diff --git a/Tests/RulesTests+Redundancy.swift b/Tests/RulesTests+Redundancy.swift index d38a10a5..b882e3ed 100644 --- a/Tests/RulesTests+Redundancy.swift +++ b/Tests/RulesTests+Redundancy.swift @@ -2919,6 +2919,22 @@ class RedundancyTests: RulesTests { testFormatting(for: input, output, rule: FormatRules.redundantReturn, options: options) } + func testNoRemoveDebugReturnFollowedBySwitch() { + let input = """ + func swiftFormatBug() -> Foo { + return .foo + + switch state { + case .foo, .bar: + return state + } + } + """ + let options = FormatOptions(swiftVersion: "5.9") + testFormatting(for: input, rule: FormatRules.redundantReturn, options: options, + exclude: ["wrapSwitchCases", "sortSwitchCases"]) + } + // MARK: - redundantBackticks func testRemoveRedundantBackticksInLet() {