swiftlang / swift-format

Formatting technology for Swift source code
Apache License 2.0
2.45k stars 224 forks source link

UseSynthesizedInitializer rules does cover default initializers #524

Open mildm8nnered opened 1 year ago

mildm8nnered commented 1 year ago

The UseSynthesizedInitializer initialiser does not seem to cover default initializers - only memberwise ones.

See https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization/#Default-Initializers

and

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization/#Memberwise-Initializers-for-Structure-Types

In code like this:

internal struct Foo {
    var bar: String = ""

    init() { }

    func myFunction() {
        _ = Self()
    }
}

the init() { } can be omitted, as it will be synthesized.

Classes will also get default initialisers under certain conditions.

ahoppen commented 4 months ago

Tracked in Apple’s issue tracker as rdar://126948248

mildm8nnered commented 4 months ago

So we actually re-implemented this rule in SwiftLint - see https://github.com/realm/SwiftLint/pull/4812

That implementation covers the case mentioned here, but also fixes a bunch of other cases. The unit test examples there should give some idea of additional coverage that swift-format does not have.