nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.62k stars 623 forks source link

Not all `init`s are initializers #1711

Closed MahdiBM closed 3 weeks ago

MahdiBM commented 1 month ago

SwiftFormat seems to take init-accessors as actual initializers:

struct MyStruct {
    private var __myVar: String
    var myVar: String {
        @storageRestrictions(initializes: __myVar)
        init { /// <---------- NOT AN INITIALIZER
            self.__myVar = newValue /// Original code makes some adjustments here
        }
        set {
            self.__myVar = newValue /// Original code makes some adjustments here
        }
        get {
            self.__myVar
        }
    }
}

This makes some rules like --self init-only affect init-accessors while they shouldn't.

Solution is to make sure SwiftFormat takes these into consideration: 1- If init does not have a () then it's not an actual initializer. 2- If init only has a single untyped parameter in the () then it's not an actual initializer. Example: init(initialValue).

Somewhat related issue: https://github.com/nicklockwood/SwiftFormat/issues/1710 Related proposal: https://github.com/apple/swift-evolution/blob/main/proposals/0400-init-accessors.md

nicklockwood commented 3 weeks ago

@MahdiBM fixed in 0.54.0