Repository navigation
Closed
Labels
bugUnexpected and reproducible misbehavior.
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
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 versionto 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 useswiftlint 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 classReacted by SkbkonturMobile, Alex Agapov and Michael Sewell
Metadata
Metadata
Assignees
Labels
bugUnexpected and reproducible misbehavior.
Activity
noahsark769 commentedon Oct 4, 2019
I'm seeing the same thing. I made the following repo with a reproducible case: https://github.com/noahsark769/Feedback-SwiftlintClosureLiteralCorrectionBug
noahsark769 commentedon Oct 4, 2019
(This still happens as of 0.35.0)
stale commentedon Nov 8, 2020
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!
noahsark769 commentedon Nov 8, 2020
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
stale commentedon Jan 7, 2021
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!
noahsark769 commentedon Jan 7, 2021
I think this is still an issue. Maybe I can take a look at it some time before the next stalebot comment
msewell commentedon Jan 28, 2021
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 theliteral_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.swiftTested with SwiftLint 0.39.2 and 0.42.0.
phoney commentedon May 11, 2021
Here's another example of code that isn't valid after correction:
Tested with swiftlint 0.43.1.
Can this be marked as a bug?
luancamara commentedon Jun 11, 2026
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]:swiftlint --fixturns the last line into], deleting2). We hit this in a real codebase wherefont: .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).