SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Bug: `indentation_width` rule does not detect incorrectly indented closing braces

Open windaishi opened this issue 9 months ago • 1 comments

New Issue Checklist

Describe the bug

The indentation_width rule fails to detect when a closing brace } is indented incorrectly (doesn't match the indentation of its opening statement). The rule correctly detects indentation issues on regular statements but misses closing braces that are under-indented.

Steps to Reproduce

Run the following command:

echo 'import SwiftUI

struct TestView: View {
    var body: some View {
        VStack {
            Text("Hello")
        }
        .onTapGesture {
            if true {
                print("inside if")
        }
        }
    }
}' | swiftlint lint --no-cache --use-stdin --enable-all-rules

File structure explained:

import SwiftUI

struct TestView: View {
    var body: some View {
        VStack {
            Text("Hello")
        }
        .onTapGesture {
            if true {                    // Line 9: 12 spaces (3 levels)
                print("inside if")       // Line 10: 16 spaces (4 levels) ✓
        }                                // Line 11: 8 spaces (2 levels) ✗ WRONG - should be 12
        }                                // Line 12: 8 spaces (2 levels) ✓
    }
}

The closing } on line 11 (closing the if statement) has only 8 spaces of indentation, but it should have 12 spaces to match the if true { on line 9.

Expected behavior

SwiftLint should report an indentation_width violation on line 11:

<nopath>:11:1: warning: Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)

Actual behavior

No violation is reported for line 11. The output only shows other violations (explicit_acl, file_header, etc.) but no indentation_width violation for the incorrectly indented closing brace.

Proof that the rule works for other cases

The rule DOES correctly detect indentation issues on regular statements:

echo 'import SwiftUI

struct TestView: View {
    var body: some View {
        if true {
          print("wrong indent")
        }
    }
}' | swiftlint lint --no-cache --use-stdin --enable-all-rules

Output includes:

<nopath>:6:1: error: Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)

This proves the rule is active and working, but it specifically misses closing braces.

Environment

  • SwiftLint version: 0.63.2
  • Installation method: Pre-built binary
  • Xcode version: 16.x
  • macOS version: macOS 15.x (Sequoia)

windaishi avatar Feb 17 '26 16:02 windaishi

I picked this up and opened #6524.

It adds a regression test for the reported SwiftUI / .onTapGesture case and tightens indentation_width for lines that start with closing delimiters so the under-indented inner brace is reported without cascading a second warning onto the outer one.

theamodhshetty avatar Mar 06 '26 07:03 theamodhshetty