nicklockwood / SwiftFormat

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

redundantInit causes "initializing from a metatype value must reference 'init' explicitly" #1672

Closed marcelofabri closed 2 months ago

marcelofabri commented 2 months ago

Given this code:

let a = Module.configuration.loggingEventBatchType.init(
   anyEvents: eventsToUpload
)

0.53.6 removes the .init, causing an error:

error: initializing from a metatype value must reference 'init' explicitly

loggingEventBatchType is defined as AnyLoggingEventBatch.Type

nicklockwood commented 2 months ago

There's currently not really a way for SwiftFormat to detect this case since it doesn't have access to type information. The best option for now would be to disable the rule, either globally or locally using:

// swiftformat:disable:next redundantInit
let a = Module.configuration.loggingEventBatchType.init(
   anyEvents: eventsToUpload
)
nicklockwood commented 2 months ago

@marcelofabri I realized on reflection that the existing heuristic I use to avoid false positives for redundantInit ought to handle this case as well, so I've pushed a fix to develop that should solve it

nicklockwood commented 2 months ago

@marcelofabri fixed in 0.53.7