← Back to Pull requests

Handle CRLF line endings when formatting and linting selections. #1336

Closed
unknown closed this pull request without merging on Nov 10, 2026 22:12 UTC
View on GitHub
unknown unknown commented on Nov 10, 2026 21:41 UTC
Author

Summary

Formatting or linting a line range (--lines) in a file with CRLF (\r\n) line endings previously stripped leading indentation on the first selected line, leaked formatting onto the line following the selection (converting its terminator to \n), and emitted false [TrailingWhitespace] lint findings:

// Input with \r\n line endings (`swift-format format --lines 2:2`):
func foo() {\r\n
    let   a   =   1\r\n
    let   b   =   2\r\n
}\r\n

// Previous output (line 2 lost indentation; line 3 was formatted):
func foo() {\r\n
let a = 1\n
  let b = 2\r\n
}\r\n

// Expected output:
func foo() {\r\n
  let a = 1\r\n
    let   b   =   2\r\n
}\r\n

Root Causes

1. In Swift's Character grapheme-cluster model, "\r\n" is a single extended grapheme cluster, so "\r\n".hasSuffix("\n") is false. When .enableFormatting copied the unselected prefix ending in \r\n via PrettyPrintBuffer.writeVerbatimAfterEnablingFormatting, str.hasSuffix("\n") evaluated to false, leaving isAtStartOfLine = false and suppressing leading indentation on the first selected line.
2. Selection.resolved(with:) computed the end of a selected line as nextLineStart.utf8Offset - 1, which in a CRLF file points between \r and \n rather than before \r\n. Because the last token on the selected line ended before \r, its end position was still < the selection's upper bound, so .disableFormatting did not fire at the end of the selected line.
3. WhitespaceLinter splits whitespace runs on \n (0x0A) only, leaving \r (0x0D) at the end of userRun while formattedRun on reformatted lines uses \n, which triggered false remove trailing whitespace findings and counted \r toward line length.

Fix

  • Check String(str).utf8.last == UInt8(ascii: "\n") and output.utf8.last == UInt8(ascii: "\n") in PrettyPrintBuffer.writeVerbatimAfterEnablingFormatting.

  • Strip both bytes of a trailing \r\n line terminator when resolving line ranges in Selection.resolved(with:), accounting for the trailing empty slice in SourceLocationConverter.sourceLines when a line range's upper bound is out of bounds.

  • Exclude \r when computing line lengths in WhitespaceLinter and trim a trailing \r before comparing pre-newline whitespace runs in checkForTrailingWhitespaceErrors.
  • Diff: 291 lines (+218 / -73) across 3 files Reviewer: dylansturg Review duration: 30m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 291 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.340
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1336"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.9% (MEDIUM) · Higher = larger blast radius
    39.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.8% · Safe to merge
    15.8%