Feature Request: Add autocorrection support for `indentation_width` rule
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
Feature Request
Description
The indentation_width rule currently does not support autocorrection (--fix). This makes it tedious to fix indentation violations manually, especially in larger codebases or when refactoring code.
According to the official documentation, the rule explicitly states:
- Supports autocorrection: No
Motivation
Indentation issues are purely formatting-related and should be safe to autocorrect. Other style rules like trailing_whitespace already support autocorrection. Adding autocorrection for indentation_width would:
- Save developers significant time fixing indentation manually
- Make the rule more practical for teams to enable
- Align with the behavior of other formatting-focused rules
Steps to Reproduce
- Create a file with incorrect indentation:
// IndentationTest.swift
import SwiftUI
struct TestView: View {
var body: some View {
HStack {
Text("Hello")
}
.frame(maxWidth: .infinity)
.onTapGesture {
if true {
Task { @MainActor in
try await doSomething() // Wrong indentation
}
}
}
}
func doSomething() async throws {}
}
- Run SwiftLint lint:
$ swiftlint lint IndentationTest.swift
Output:
IndentationTest.swift:13:1: error: Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)
- Attempt to fix with
--fix:
$ swiftlint --fix IndentationTest.swift
Output:
Correcting Swift files at paths IndentationTest.swift
Correcting 'IndentationTest.swift' (1/1)
Done correcting 1 file!
- Run lint again - the
indentation_widthviolation still exists:
$ swiftlint lint IndentationTest.swift
Output:
IndentationTest.swift:13:1: error: Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)
Expected Behavior
Running swiftlint --fix should automatically correct indentation violations by adjusting the whitespace at the beginning of lines to match the expected indentation level.
Environment
- SwiftLint version: 0.63.2
- Installation method: Pre-built binary
- Xcode version: 16.x
- macOS version: macOS 15.x (Sequoia)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the implementation and tests for the indentation_width rule, then reproduce the reported case with SwiftLint 0.63.2 and --fix. Done means --fix adjusts leading whitespace for indentation_width violations and a subsequent lint run reports no remaining violation, with regression coverage for the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100