Skip to content

Fix literal_expression_end_indentation correction deleting code - #6769

Merged
SimplyDanny merged 1 commit into
realm:mainfrom
luancamara:fix-literal-expression-end-indentation-correction
Jun 14, 2026
Merged

SimplyDanny merged 1 commit into
realm:mainfrom
luancamara:fix-literal-expression-end-indentation-correction

Conversation

@luancamara

@luancamara luancamara commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2823.

(The fix targets the rule's corrector; detection behavior is unchanged.)

Bug

When the closing bracket of a multiline literal shares a line with the end of a multiline last element, the literal_expression_end_indentation corrector deletes source code and produces non-compiling output.

Minimal reproduction (SwiftLint 0.63.3, config only_rules: [literal_expression_end_indentation]):

let x = [
   foo(
      1,
      2)]

swiftlint --fix rewrites the last line to ], deleting 2). Real-world case we hit: font: .caption(.regular))], became ], — an entire argument silently dropped. We only caught it because the parentheses became unbalanced; a balanced collapse would have been a silent behavior change.

Root cause

correct(contents:expected:actual:) assumes everything before the bracket on its line is indentation and replaces that whole range with the expected indentation. The violation geometry (actualRange covers the full line prefix before ]) admits ranges containing code when the last element is multiline.

Fix

Only perform the in-place replacement when the range is all whitespace; otherwise insert a newline plus the expected indentation right before the bracket, moving just the bracket to its own line. Detection behavior is unchanged; the rule's built-in re-correct pass converges (verified idempotent).

Testing

  • New triggering and correction examples in the rule covering the multiline-last-element pattern.
  • swift test --filter LiteralExpressionEndIndentation passes.
  • End-to-end with the patched binary on the original repro: content preserved, only the bracket moves, and a second --fix run makes no changes.

The corrector assumed everything before the closing bracket on its line
was indentation and replaced that whole range with the expected
indentation. When a multiline last element ends on the same line as the
bracket (e.g. `...))]`), the range contains source code, which got
deleted and produced non-compiling output.

Only perform the in-place replacement when the range is all whitespace;
otherwise insert a newline plus the expected indentation right before
the bracket, moving just the bracket to its own line.

Fixes the corrector half of realm#2823.
@SwiftLintBot

Copy link
Copy Markdown
19 Messages
📖 Building this branch resulted in a binary size of 27554.74 KiB vs 27554.6 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.15 s vs 0.17 s on main (11% faster).
📖 Linting Alamofire with this PR took 0.19 s vs 0.23 s on main (17% faster).
📖 Linting Brave with this PR took 0.99 s vs 1.02 s on main (2% faster).
📖 Linting DuckDuckGo with this PR took 4.42 s vs 4.43 s on main (0% faster).
📖 Linting Firefox with this PR took 1.78 s vs 1.86 s on main (4% faster).
📖 Linting Kickstarter with this PR took 1.18 s vs 1.17 s on main (0% slower).
📖 Linting Moya with this PR took 0.13 s vs 0.12 s on main (8% slower).
📖 Linting NetNewsWire with this PR took 0.42 s vs 0.43 s on main (2% faster).
📖 Linting Nimble with this PR took 0.17 s vs 0.21 s on main (19% faster).
📖 Linting PocketCasts with this PR took 1.2 s vs 1.18 s on main (1% slower).
📖 Linting Quick with this PR took 0.17 s vs 0.14 s on main (21% slower).
📖 Linting Realm with this PR took 0.49 s vs 0.51 s on main (3% faster).
📖 Linting Sourcery with this PR took 0.31 s vs 0.31 s on main (0% slower).
📖 Linting Swift with this PR took 0.54 s vs 0.54 s on main (0% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 3.91 s vs 3.92 s on main (0% faster).
📖 Linting VLC with this PR took 0.27 s vs 0.27 s on main (0% slower).
📖 Linting Wire with this PR took 2.42 s vs 2.42 s on main (0% slower).
📖 Linting WordPress with this PR took 1.73 s vs 1.71 s on main (1% slower).

Generated by 🚫 Danger

@SimplyDanny SimplyDanny left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @luancamara!

@SimplyDanny
SimplyDanny enabled auto-merge (squash) June 14, 2026 20:56
@SimplyDanny
SimplyDanny merged commit 310e0da into realm:main Jun 14, 2026
19 checks passed
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.

Autocorrection with literal_expression_end_indentation results in uncompilable code

3 participants