Skip to content

Conversation

@ldct
Copy link

@ldct ldct commented Feb 11, 2026

Summary

This PR adds autocorrection support to the conditional_returns_on_newline rule.

Changes

  • Added a Rewriter class to automatically fix violations where return statements are on the same line as conditional keywords (if/guard)
  • The autocorrection:
    • Adds a newline before the return statement
    • Preserves proper indentation (4 spaces from the conditional's indentation)
    • Handles both if/else and guard statements
    • Preserves preceding statements on the same line (e.g., XCTFail();)
    • Respects the if_only configuration option
  • Added test for guard statement corrections

Example Corrections

// Before
guard true else { return }

// After
guard true else {
    return
}
// Before
guard condition else { XCTFail(); return }

// After
guard condition else { XCTFail();
    return
}

Added a Rewriter class to automatically fix violations where return
statements are on the same line as conditional keywords (if/guard).

The autocorrection:
- Adds a newline before the return statement
- Preserves proper indentation (4 spaces from the conditional's indentation)
- Handles both if/else and guard statements
- Preserves preceding statements on the same line (e.g., XCTFail())
- Respects the if_only configuration option

Added test for guard statement corrections.

Co-authored-by: Shelley <shelley@exe.dev>
@SwiftLintBot
Copy link

SwiftLintBot commented Feb 11, 2026

1 Warning
⚠️ If this is a user-facing change, please include a CHANGELOG entry to credit yourself!
You can find it at CHANGELOG.md.
19 Messages
📖 Building this branch resulted in a binary size of 27257.65 KiB vs 27252.48 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.82 s vs 0.81 s on main (1% slower).
📖 Linting Alamofire with this PR took 1.09 s vs 1.07 s on main (1% slower).
📖 Linting Brave with this PR took 7.27 s vs 7.23 s on main (0% slower).
📖 Linting DuckDuckGo with this PR took 25.34 s vs 25.41 s on main (0% faster).
📖 Linting Firefox with this PR took 11.72 s vs 11.73 s on main (0% faster).
📖 Linting Kickstarter with this PR took 8.06 s vs 8.08 s on main (0% faster).
📖 Linting Moya with this PR took 0.51 s vs 0.47 s on main (8% slower).
📖 Linting NetNewsWire with this PR took 2.47 s vs 2.47 s on main (0% slower).
📖 Linting Nimble with this PR took 0.7 s vs 0.7 s on main (0% slower).
📖 Linting PocketCasts with this PR took 7.55 s vs 7.67 s on main (1% faster).
📖 Linting Quick with this PR took 0.47 s vs 0.47 s on main (0% slower).
📖 Linting Realm with this PR took 3.29 s vs 3.27 s on main (0% slower).
📖 Linting Sourcery with this PR took 1.88 s vs 1.9 s on main (1% faster).
📖 Linting Swift with this PR took 4.57 s vs 4.52 s on main (1% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 0.35 s vs 0.35 s on main (0% slower).
📖 Linting VLC with this PR took 1.18 s vs 1.17 s on main (0% slower).
📖 Linting Wire with this PR took 18.54 s vs 18.55 s on main (0% faster).
📖 Linting WordPress with this PR took 12.56 s vs 12.55 s on main (0% slower).

Here's an example of your CHANGELOG entry:

* Make conditional_returns_on_newline rule autocorrectable.  
  [ldct](https://github.com/ldct)
  [#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)

note: There are two invisible spaces after the entry's text.

Generated by 🚫 Danger

exe.dev user and others added 3 commits February 11, 2026 00:34
Co-authored-by: Shelley <shelley@exe.dev>
…orrectable

Co-authored-by: Shelley <shelley@exe.dev>
Co-authored-by: Shelley <shelley@exe.dev>
Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a highly appreciated enhancement. Thanks for the contribution!

Example("↓if true { break } else { return }"):
Example("if true { break } else {\n return\n}"),
Example("↓if true { return \"YES\" } else { return \"NO\" }"):
Example("if true {\n return \"YES\"\n} else { return \"NO\" }"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add another example where the if is indented, e.g.

func f() {
    if true { return }
}


func testGuardCorrection() {
// Test guard correction with default configuration
let corrections = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be part of the example list in the rule itself. Make sure, that violating guard examples don't get fixed when the if_only option is set.

exe.dev user and others added 2 commits February 13, 2026 00:46
Co-authored-by: Shelley <shelley@exe.dev>
- Added guard correction examples to the rule's corrections array
- Updated if_only test to override corrections (excluding guard)
- Removed separate testGuardCorrection since it's now tested via RuleDescription

Co-authored-by: Shelley <shelley@exe.dev>
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.

3 participants