From dd58b90b3eee4d32c9ea375dca51ad4d184017f0 Mon Sep 17 00:00:00 2001 From: theamodhshetty Date: Wed, 18 Mar 2026 10:39:55 +0530 Subject: [PATCH] fix(control_statement): skip commented paren corrections --- CHANGELOG.md | 5 +++++ .../Rules/Style/ControlStatementRule.swift | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 612e577b77..b782e1c7ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,11 @@ ### Bug Fixes +* Skip `control_statement` corrections for parenthesized conditions that contain + comments, avoiding comment loss during `swiftlint --fix`. + [theamodhshetty](https://github.com/theamodhshetty) + [#6207](https://github.com/realm/SwiftLint/issues/6207) + * Add an `ignore_attributes` option to `implicit_optional_initialization` so wrappers/attributes that require explicit `= nil` can be excluded from style checks for both `style: always` and `style: never`. diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift index 290837dfa7..4e922f217e 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift @@ -20,6 +20,11 @@ struct ControlStatementRule: Rule { Example("renderGif(data)"), Example("guard condition else {}"), Example("while condition {}"), + Example(""" + if (abc == 1/* && cdf == 2*/) { + print("Hello, world!") + } + """), Example("do {} while condition {}"), Example("do { ; } while condition {}"), Example("switch foo {}"), @@ -169,8 +174,16 @@ private extension ControlStatementRule { private extension ExprSyntax { var unwrapped: ExprSyntax? { - if let expr = `as`(TupleExprSyntax.self)?.elements.onlyElement?.expression { - return containsTrailingClosure(Syntax(expr)) ? nil : expr + if let tuple = `as`(TupleExprSyntax.self), + let expr = tuple.elements.onlyElement?.expression { + if containsTrailingClosure(Syntax(expr)) || + expr.leadingTrivia.containsComments || + expr.trailingTrivia.containsComments || + tuple.leftParen.trailingTrivia.containsComments || + tuple.rightParen.leadingTrivia.containsComments { + return nil + } + return expr } return nil }