Skip to content

Autocorrection with closure_end_indentation results in uncompilable code #2686

Closed

Description

@fbernutz

New Issue Checklist

Describe the bug

Using swiftlint autocorrect with closure_end_indentation in a nested UIView.animate closure results in uncompilable code.

Complete output when running SwiftLint, including the stack trace and command used
Loading configuration from '.swiftlint.yml'
Linting Swift files at paths 
Linting 'ViewController.swift' (1/2)
Linting 'AppDelegate.swift' (2/2)
/Users/feliziabernutz/develop/_custom/swiftlint-formatting-bug/swiftlint-formatting-bug/ViewController.swift:26:13: warning: Closure End Indentation Violation: Closure end should have the same indentation as the line that started it. Expected 16, got 12. (closure_end_indentation)
/Users/feliziabernutz/develop/_custom/swiftlint-formatting-bug/swiftlint-formatting-bug/ViewController.swift:19:9: warning: Closure End Indentation Violation: Closure end should have the same indentation as the line that started it. Expected 12, got 8. (closure_end_indentation)
Done linting! Found 2 violations, 0 serious in 2 files.

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.31.0
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? CocoaPods
  • Paste your configuration file:
opt_in_rules:
  - closure_end_indentation
excluded:
  - Pods
  • 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)? Xcode 10.1
  • 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.

swiftlint lint identifies a Closure End Indentation Violation but the autocorrect command is incorrect.

Run swiftlint autocorrect on this code:

    func animate(completion: (() -> Void)? = nil) {
        UIView.animate(
            withDuration: 0.4,
            delay: 0.4,
            options: [.curveEaseOut],
            animations: {
                print("first animation")
        }, completion: { _ in
            UIView.animate(
                withDuration: 0.2,
                delay: 0.0,
                options: [.curveEaseOut],
                animations: {
                    print("second animation")
            }, completion: { _ in
                completion?()
            })
        })
    }

results in this:

    func animate(completion: (() -> Void)? = nil) {
        UIView.animate(
            withDuration: 0.4,
            delay: 0.4,
            options: [.curveEaseOut],
            animations: {
                print("first animation")
            }, completion: { _ in
            UIView.animate(
                withDuration: 0.2,
                delay: 0.0,
                options: [.curveEaseOut],
                animations: {
                    print("second animation")
        
t], // Compiler error: Consecutive statements on a line must be separated by ';', Use of unresolved identifier 't'; did you mean 'tm'?, Expected expression
                }, completion: { _ in
                completion?()
                })
            })
    }

Seems like this has something to do with putting each argument in new lines.
This autocorrects correctly:

    func animate(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.4, delay: 0.4, options: [.curveEaseOut], animations: {
            print("first animation")
        }, completion: { _ in
            UIView.animate(withDuration: 0.2, delay: 0.0, options: [.curveEaseOut], animations: {
                 print("second animation")
            }, completion: { _ in
                completion?()
            })
        })
    }

Here is a demo Xcode project with a build script phase which runs swiftlint autocorrect and when building the project Swiftlint "corrects" the described function above in ViewController.swift: swiftlint-formatting-bug.zip

Activity

  1. added
    bug
    on Apr 1, 2019
  2. 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!

  3. added
    wontfix
    on Nov 8, 2020
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

    bugwontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions