diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 7aea2f5b..c555d4c1 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -6263,6 +6263,7 @@ public struct _FormatRules { case (nil, nil): potentialStartOfExpressionContainingClosure = nil case (.some(let startOfScope), nil): + guard formatter.tokens[startOfScope] == .startOfScope("{") else { return } potentialStartOfExpressionContainingClosure = startOfScope case (nil, let .some(assignmentBeforeClosure)): potentialStartOfExpressionContainingClosure = assignmentBeforeClosure diff --git a/Tests/RulesTests+Redundancy.swift b/Tests/RulesTests+Redundancy.swift index 4b12aedd..b443908f 100644 --- a/Tests/RulesTests+Redundancy.swift +++ b/Tests/RulesTests+Redundancy.swift @@ -8485,6 +8485,70 @@ class RedundancyTests: RulesTests { testFormatting(for: input, rule: FormatRules.redundantClosure, options: options) } + func testClosureNotRemovedInMethodCall() { + let input = """ + XCTAssert({ + if foo { + bar + } else { + baaz + } + }()) + """ + + let options = FormatOptions(swiftVersion: "5.9") + testFormatting(for: input, rule: FormatRules.redundantClosure, options: options) + } + + func testClosureNotRemovedInMethodCall2() { + let input = """ + method("foo", { + if foo { + bar + } else { + baaz + } + }()) + """ + + let options = FormatOptions(swiftVersion: "5.9") + testFormatting(for: input, rule: FormatRules.redundantClosure, options: options) + } + + func testClosureNotRemovedInMethodCall3() { + let input = """ + XCTAssert({ + if foo { + bar + } else { + baaz + } + }(), "message") + """ + + let options = FormatOptions(swiftVersion: "5.9") + testFormatting(for: input, rule: FormatRules.redundantClosure, options: options) + } + + func testClosureNotRemovedInMethodCall4() { + let input = """ + method( + "foo", + { + if foo { + bar + } else { + baaz + } + }(), + "bar" + ) + """ + + let options = FormatOptions(swiftVersion: "5.9") + testFormatting(for: input, rule: FormatRules.redundantClosure, options: options) + } + // MARK: Redundant optional binding func testRemovesRedundantOptionalBindingsInSwift5_7() {