From 88424eeeb920d08dd0d9a32801533dbc7325fc76 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 13 Apr 2026 19:32:03 -0700 Subject: [PATCH] Fix redundantSelf crash on switch expression inside if-let binding chain (#2506) Co-authored-by: calda <1811727+calda@users.noreply.github.com> --- Sources/ParsingHelpers.swift | 3 +- Tests/Rules/RedundantSelfTests.swift | 44 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/Sources/ParsingHelpers.swift b/Sources/ParsingHelpers.swift index 86e83c19..30f6c57a 100644 --- a/Sources/ParsingHelpers.swift +++ b/Sources/ParsingHelpers.swift @@ -936,7 +936,8 @@ extension Formatter { !nextToken.isOperator(ofType: .infix), !nextToken.isOperator(ofType: .postfix), nextToken != .startOfScope("("), - nextToken != .startOfScope("{") + nextToken != .startOfScope("{"), + nextToken != .delimiter(",") else { return isAfterBrace(index, braceIndex) } diff --git a/Tests/Rules/RedundantSelfTests.swift b/Tests/Rules/RedundantSelfTests.swift index 148c3a83..749759b1 100644 --- a/Tests/Rules/RedundantSelfTests.swift +++ b/Tests/Rules/RedundantSelfTests.swift @@ -4825,4 +4825,48 @@ final class RedundantSelfTests: XCTestCase { """ testFormatting(for: input, rule: .redundantSelf, exclude: [.blankLinesAfterGuardStatements]) } + + func testRedundantSelfWithSwitchExpressionInIfLetBindingChain() { + let input = """ + class Foo { + var value: Int = 0 + func foo(someOptional: Bool?) { + if let value = someOptional, + let result: String? = switch value { + case true: "hello" + case false: "world" + }, let result + { + print(result) + print(self.value) + } + } + } + """ + let options = FormatOptions(swiftVersion: "5.9") + testFormatting(for: input, rule: .redundantSelf, options: options, + exclude: [.braces, .indent, .wrapMultilineConditionalAssignment]) + } + + func testRedundantSelfWithSwitchExpressionInGuardLetBindingChain() { + let input = """ + class Foo { + var value: Int = 0 + func foo(someOptional: Bool?) { + guard let value = someOptional else { return } + guard let result: String? = switch value { + case true: "hello" + case false: nil + }, let result else { return } + print(result) + print(self.value) + } + } + """ + let options = FormatOptions(swiftVersion: "5.9") + testFormatting(for: input, rule: .redundantSelf, options: options, + exclude: [.blankLinesAfterGuardStatements, + .wrapConditionalBodies, + .wrapMultilineConditionalAssignment]) + } }