Skip to content

Feature Request: Add autocorrection support for indentation_width rule #6497

@windaishi

Description

@windaishi

New Issue Checklist

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:

  1. Save developers significant time fixing indentation manually
  2. Make the rule more practical for teams to enable
  3. Align with the behavior of other formatting-focused rules

Steps to Reproduce

  1. 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 {}
}
  1. 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)
  1. 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!
  1. Run lint again - the indentation_width violation 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementIdeas for improvements of existing features and rules.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions