File tree 2 files changed +23
-4
lines changed
Tests/SwiftFormatRulesTests 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -30,10 +30,16 @@ public final class NoParensAroundConditions: SyntaxFormatRule {
30
30
assert ( tuple. elementList. count == 1 )
31
31
let expr = tuple. elementList. first!. expression
32
32
33
- // If the condition is a function with a trailing closure, removing the
34
- // outer set of parentheses introduces a parse ambiguity.
35
- if let fnCall = expr. as ( FunctionCallExprSyntax . self) , fnCall. trailingClosure != nil {
36
- return ExprSyntax ( tuple)
33
+ // If the condition is a function with a trailing closure or if it's an immediately called
34
+ // closure, removing the outer set of parentheses introduces a parse ambiguity.
35
+ if let fnCall = expr. as ( FunctionCallExprSyntax . self) {
36
+ if fnCall. trailingClosure != nil {
37
+ // Leave parentheses around call with trailing closure.
38
+ return ExprSyntax ( tuple)
39
+ } else if fnCall. calledExpression. as ( ClosureExprSyntax . self) != nil {
40
+ // Leave parentheses around immediately called closure.
41
+ return ExprSyntax ( tuple)
42
+ }
37
43
}
38
44
39
45
diagnose ( . removeParensAroundExpression, on: expr)
Original file line number Diff line number Diff line change @@ -160,4 +160,17 @@ final class NoParensAroundConditionsTests: LintOrFormatRuleTestCase {
160
160
}
161
161
""" )
162
162
}
163
+
164
+ func testParensAroundAmbiguousConditions( ) {
165
+ XCTAssertFormatting (
166
+ NoParensAroundConditions . self,
167
+ input: """
168
+ if ({ true }()) {}
169
+ if (functionWithTrailingClosure { 5 }) {}
170
+ """ ,
171
+ expected: """
172
+ if ({ true }()) {}
173
+ if (functionWithTrailingClosure { 5 }) {}
174
+ """ )
175
+ }
163
176
}
You can’t perform that action at this time.
0 commit comments