Skip to content

Commit 0e9a5b9

Browse files
authored
Merge pull request swiftlang#380 from chuckluck44/fix/where-clause-else-check
2 parents f30e81e + 793d764 commit 0e9a5b9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Sources/SwiftFormatRules/UseWhereClausesInForLoops.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public final class UseWhereClausesInForLoops: SyntaxFormatRule {
4141
// with a single condition whose body is just `continue`.
4242
switch stmt.item.as(SyntaxEnum.self) {
4343
case .ifStmt(let ifStmt)
44-
where ifStmt.conditions.count == 1 && node.body.statements.count == 1:
44+
where ifStmt.conditions.count == 1 && ifStmt.elseKeyword == nil
45+
&& node.body.statements.count == 1:
4546
// Extract the condition of the IfStmt.
4647
let conditionElement = ifStmt.conditions.first!
4748
guard let condition = conditionElement.condition.as(ExprSyntax.self) else {

Tests/SwiftFormatRulesTests/UseWhereClausesInForLoopsTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ final class UseWhereClausesInForLoopsTests: LintOrFormatRuleTestCase {
1111
}
1212
}
1313
14+
for i in [0, 1, 2, 3] {
15+
if i > 30 {
16+
print(i)
17+
} else {
18+
print(i)
19+
}
20+
}
21+
22+
for i in [0, 1, 2, 3] {
23+
if i > 30 {
24+
print(i)
25+
} else if i > 40 {
26+
print(i)
27+
}
28+
}
29+
1430
for i in [0, 1, 2, 3] {
1531
if i > 30 {
1632
print(i)
@@ -36,6 +52,22 @@ final class UseWhereClausesInForLoopsTests: LintOrFormatRuleTestCase {
3652
print(i)
3753
}
3854
55+
for i in [0, 1, 2, 3] {
56+
if i > 30 {
57+
print(i)
58+
} else {
59+
print(i)
60+
}
61+
}
62+
63+
for i in [0, 1, 2, 3] {
64+
if i > 30 {
65+
print(i)
66+
} else if i > 40 {
67+
print(i)
68+
}
69+
}
70+
3971
for i in [0, 1, 2, 3] {
4072
if i > 30 {
4173
print(i)

0 commit comments

Comments
 (0)