Fix redundantSelf crash on switch expression inside if-let binding chain (#2506)

Co-authored-by: calda <1811727+calda@users.noreply.github.com>
This commit is contained in:
Copilot
2026-04-13 19:32:03 -07:00
committed by GitHub
parent aceab79832
commit 88424eeeb9
2 changed files with 46 additions and 1 deletions
+2 -1
View File
@@ -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)
}
+44
View File
@@ -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])
}
}