realm / realm/SwiftLint

Feature Request: Add autocorrection support for `indentation_width` rule

Open
#6,497 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.