Fix early return statement

This commit is contained in:
Nick Lockwood
2023-09-02 09:02:32 +01:00
parent 1780bcdc32
commit 13e95b7e0d
2 changed files with 19 additions and 2 deletions
+3 -2
View File
@@ -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)
+16
View File
@@ -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() {