nicklockwood / SwiftFormat

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

Initializing from a metatype value must reference 'init' explicitly #1175

Closed Cyberbeni closed 2 years ago

Cyberbeni commented 2 years ago

I'm testing SE-0309 with the 5.7 toolchain and ran into a problem with swiftformat, that is also present for earlier swift versions (only tested with the newest one included in Xcode 13.3.1). init gets removed in the below example resulting in the following compiler error: Initializing from a metatype value must reference 'init' explicitly

let Type = NSObject.self
let object = Type.init()
nicklockwood commented 2 years ago

@Cyberbeni this is mentioned in the README:

If you assign SomeClass.self to a variable and then instantiate an instance of the class using that variable, Swift requires that you use an explicit .init(), however, the redundantInit rule is not currently capable of detecting this situation and will remove the .init. To work around this issue, use the // swiftformat:disable:next redundantInit comment directive to disable the rule for any affected lines of code (or just disable the redundantInit rule completely).

nicklockwood commented 2 years ago

I probably could detect simple cases where the type is declared locally though - I'll see if I can improve this behavior a bit.

Cyberbeni commented 2 years ago

Ah, there is also already an issue with the same name 🙈 #299