SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Avoid dropping inline comments in control_statement fixes

Open theamodhshetty opened this issue 8 months ago • 3 comments

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

theamodhshetty avatar Mar 18 '26 05:03 theamodhshetty

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

Generated by :no_entry_sign: Danger

SwiftLintBot avatar Mar 18 '26 05:03 SwiftLintBot

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

SimplyDanny avatar Apr 10 '26 19:04 SimplyDanny

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

theamodhshetty avatar Apr 17 '26 05:04 theamodhshetty