mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user