Skip to content

Avoid dropping inline comments in control_statement fixes#6545

Open
theamodhshetty wants to merge 2 commits intorealm:mainfrom
theamodhshetty:codex/fix-control-statement-inline-comments
Open

Avoid dropping inline comments in control_statement fixes#6545
theamodhshetty wants to merge 2 commits intorealm:mainfrom
theamodhshetty:codex/fix-control-statement-inline-comments

Conversation

@theamodhshetty
Copy link
Copy Markdown
Contributor

Summary

  • stop control_statement from unwrapping parenthesized conditions when the parentheses contain comments
  • add a regression example for the inline-comment case from the issue
  • add the required changelog entry

Why

swiftlint --fix was deleting inline comments in cases like if (abc == 1/* ... */) {}. This change takes the conservative path and skips those corrections instead of applying a destructive rewrite.

Testing

  • swift test --filter ControlStatementRuleGeneratedTests
  • ./.build/debug/swiftlint lint Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift CHANGELOG.md --strict
  • ./.build/debug/swiftlint lint --fix --only-rule control_statement /tmp/control_statement_comment.swift

Closes #6207

@SwiftLintBot
Copy link
Copy Markdown

SwiftLintBot commented Mar 18, 2026

1 Warning
⚠️ This PR may need tests.
19 Messages
📖 Building this branch resulted in a binary size of 27342.37 KiB vs 27357.74 KiB when built on main (-1% smaller).
📖 Linting Aerial with this PR took 0.2 s vs 0.2 s on main (0% slower).
📖 Linting Alamofire with this PR took 0.2 s vs 0.2 s on main (0% slower).
📖 Linting Brave with this PR took 0.84 s vs 0.85 s on main (1% faster).
📖 Linting DuckDuckGo with this PR took 3.54 s vs 3.56 s on main (0% faster).
📖 Linting Firefox with this PR took 1.55 s vs 1.57 s on main (1% faster).
📖 Linting Kickstarter with this PR took 0.99 s vs 0.96 s on main (3% slower).
📖 Linting Moya with this PR took 0.15 s vs 0.1 s on main (49% slower).
📖 Linting NetNewsWire with this PR took 0.36 s vs 0.32 s on main (12% slower).
📖 Linting Nimble with this PR took 0.16 s vs 0.16 s on main (0% slower).
📖 Linting PocketCasts with this PR took 0.93 s vs 0.94 s on main (1% faster).
📖 Linting Quick with this PR took 0.1 s vs 0.16 s on main (37% faster).
📖 Linting Realm with this PR took 0.39 s vs 0.37 s on main (5% slower).
📖 Linting Sourcery with this PR took 0.33 s vs 0.3 s on main (10% slower).
📖 Linting Swift with this PR took 0.49 s vs 0.46 s on main (6% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 3.84 s vs 3.85 s on main (0% faster).
📖 Linting VLC with this PR took 0.26 s vs 0.25 s on main (4% slower).
📖 Linting Wire with this PR took 2.04 s vs 2.07 s on main (1% faster).
📖 Linting WordPress with this PR took 1.38 s vs 1.39 s on main (0% faster).

Generated by 🚫 Danger

@SimplyDanny
Copy link
Copy Markdown
Collaborator

Why not remove the parentheses while keeping the comment as is?

@theamodhshetty
Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. I pushed ba5b107 so the correction removes the outer parentheses and keeps inline comments in place instead of skipping those cases.

I also checked the same trivia path for guard, while, and switch locally so the fix does not only work for the added if example.

Validation rerun:

  • swift test --filter ControlStatementRuleGeneratedTests
  • ./.build/debug/swiftlint lint Source/SwiftLintBuiltInRules/Rules/Style/ControlStatementRule.swift CHANGELOG.md --strict

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

control_statement kills inline comments

3 participants