From a962548ecf8f7bd3b4131bffb01173fe24d6ae69 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Thu, 26 Feb 2026 09:21:21 +0000 Subject: [PATCH] Fix incorrect replacement of && operator inside a ternary condition --- Sources/Rules/AndOperator.swift | 2 +- Tests/Rules/AndOperatorTests.swift | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/Rules/AndOperator.swift b/Sources/Rules/AndOperator.swift index 0222b003..04668fa7 100644 --- a/Sources/Rules/AndOperator.swift +++ b/Sources/Rules/AndOperator.swift @@ -35,7 +35,7 @@ public extension FormatRule { let endOfGroup = formatter.index(of: .delimiter(","), after: index) ?? endIndex var nextOpIndex = index while let next = formatter.index(of: .operator, after: nextOpIndex) { - if formatter.tokens[next] == .operator("||", .infix) { + if [.operator("||", .infix), .operator("?", .infix)].contains(formatter.tokens[next]) { index = endOfGroup continue outer } diff --git a/Tests/Rules/AndOperatorTests.swift b/Tests/Rules/AndOperatorTests.swift index 595b870a..6b3fce82 100644 --- a/Tests/Rules/AndOperatorTests.swift +++ b/Tests/Rules/AndOperatorTests.swift @@ -134,6 +134,13 @@ final class AndOperatorTests: XCTestCase { testFormatting(for: input, rule: .andOperator) } + func testNoReplaceAndInTernaryCondition() { + let input = """ + if foo && bar ? baz : quux {} + """ + testFormatting(for: input, rule: .andOperator) + } + func testHandleAndAtStartOfLine() { let input = """ if a == b