Skip to content

Autocorrection with literal_expression_end_indentation results in uncompilable code #2823

Closed

Description

@setoff

New Issue Checklist

Describe the bug

Using swiftlint autocorrect with literal_expression_end_indentation in complex expression results in uncompilable code.
Might be related to #2686

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint
Loading configuration from '.swiftlint.yml'
Linting Swift files at paths 
Linting 'ViewController.swift' (1/2)
Linting 'AppDelegate.swift' (2/2)
/<path redacted>/ViewController.swift:12:9: warning: Literal Expression End Indentation Violation: Array and dictionary literal end should have the same indentation as the line that started it. Expected 4, got 8. (literal_expression_end_indentation)
Done linting! Found 1 violation, 0 serious in 2 files.

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.34.0
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? Homebrew
  • Paste your configuration file:
opt_in_rules:
  - literal_expression_end_indentation
  • Are you using nested configurations? No
    If so, paste their relative paths and respective contents.
  • Which Xcode version are you using (check xcode-select -p)? Version 10.2.1 (10E1001)
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.

Run swift autocorrect on this code:

import UIKit

class ViewController: UIViewController {
  struct SomeStuct {
    let setup: (Int) -> Void
  }

  private func build() -> [SomeStuct] {
    return [
      SomeStuct(setup: { val in
        print("val", val)
      })]
  }

}

swiftlint autocorrect output:

$ swiftlint autocorrect
Loading configuration from '.swiftlint.yml'
Correcting Swift files at paths 
Correcting 'ViewController.swift' (1/2)
Correcting 'AppDelegate.swift' (2/2)
/<path redacted>/ViewController.swift:12:1 Corrected Literal Expression End Indentation
/<path redacted>/ViewController.swift:13:1 Corrected Literal Expression End Indentation
Done correcting 2 files!

Resulting code:

import UIKit

class ViewController: UIViewController {
  struct SomeStuct {
    let setup: (Int) -> Void
  }

  private func build() -> [SomeStuct] {
    return [
      SomeStuct(setup: { val in
        print("val", val)
    ] // compile error: Expected expression
    } // compile error: Expected ']' in container literal expression

} //compile error: Expected ')' in expression list
// compile error: Expected '}' in class

Activity

  1. noahsark769 commented on Oct 4, 2019

    @noahsark769
    Contributor
  2. noahsark769 commented on Oct 4, 2019

    @noahsark769
    Contributor

    (This still happens as of 0.35.0)

  3. stale commented on Nov 8, 2020

    @stale

    This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

  4. added
    wontfix
    on Nov 8, 2020
  5. noahsark769 commented on Nov 8, 2020

    @noahsark769
    Contributor

    I believe this is still an issue, so makes sense to keep it open. However, it looks like the impact has been lessened by #3343

  6. removed
    wontfix
    on Nov 8, 2020
  7. stale commented on Jan 7, 2021

    @stale

    This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

  8. added
    wontfix
    on Jan 7, 2021
  9. noahsark769 commented on Jan 7, 2021

    @noahsark769
    Contributor

    I think this is still an issue. Maybe I can take a look at it some time before the next stalebot comment

  10. removed
    wontfix
    on Jan 7, 2021
  11. msewell commented on Jan 28, 2021

    @msewell

    Adding to this issue with a piece of code that actually causes SwiftLint to not only produce un-compilable code, but actually choke (bus error) when autocorrecting using the literal_expression_end_indentation:

    $ swiftlint autocorrect --config .swiftlint.yml failing.swift
    Correcting Swift files at paths failing.swift
    Correcting 'failing.swift' (1/1)
    [1]    60796 bus error  swiftlint autocorrect --config .swiftlint.yml failing.swift
    // failing.swift
    func someFunc() -> [AnyHashable: Any] {
        return ["someKey" : [
            "someOtherKey" : [
                "collection": [[String: Any]]()
            ]]]
    }
    # swiftlint.yml
    only_rules:
      - literal_expression_end_indentation

    Tested with SwiftLint 0.39.2 and 0.42.0.

  12. phoney commented on May 11, 2021

    @phoney

    Here's another example of code that isn't valid after correction:

    public let dateInputValidator = Validator<String>(rules: [
        ValidationRule<String>(userMessage: "Must be a valid date.") { input in
            return false
        }])
    

    Tested with swiftlint 0.43.1.
    Can this be marked as a bug?

  13. added
    bug
    on May 1, 2022
  14. luancamara commented on Jun 11, 2026

    @luancamara
    Contributor

    Still reproducible on 0.63.3 (latest release) — and the failure mode can be worse than wrong indentation: the corrector silently deletes source code.

    Minimal repro, valid input file, config only_rules: [literal_expression_end_indentation]:

    let x = [
       foo(
          1,
          2)]

    swiftlint --fix turns the last line into ], deleting 2). We hit this in a real codebase where font: .caption(.regular))], became ], — an entire argument dropped. We only caught it because the parentheses became unbalanced; if the collapsed result had been syntactically valid, this would have been a silent behavior change.

    Root cause: the corrector replaces the full line prefix before ] assuming it is indentation, but when a multiline last element ends on the bracket's line, that range contains code.

    Opened #6769 with a fix (whitespace-guard + move only the bracket to its own line).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions