Swiftformat: Initializing from a metatype value must reference 'init' explicitly

Created on 2 Sep 2018  路  2Comments  路  Source: nicklockwood/SwiftFormat

Formatting

struct Foo {}

let Bar = Foo.self
let bar = Bar.init()

results in

struct Foo {}

let Bar = Foo.self
let bar = Bar() // 馃洃 Initializing from a metatype value must reference 'init' explicitly
known issue

Most helpful comment

@PabloSichert I've added a known issue about this in the README file, with the suggested workaround.

Fixing it for the general case will not be possible because SwiftFormat doesn't have enough contextual information to detect legitimate vs illegitimate use cases. I might be able to handle the common cases so that there are fewer false positives though (e.g. where the metatype variable is defined locally in the same scope as it is used).

I don't think this comes up often enough to be worth disabling the rule by default, but I'm open to doing that if there are many more reports. I'll leave the ticket open for now.

All 2 comments

@PabloSichert thanks for reporting this. I'll see what I can do to fix it. In the meantime, adding

// swiftformat:disable:next redundantInit

immediately above the line let bar = Bar.init() should solve the problem.

@PabloSichert I've added a known issue about this in the README file, with the suggested workaround.

Fixing it for the general case will not be possible because SwiftFormat doesn't have enough contextual information to detect legitimate vs illegitimate use cases. I might be able to handle the common cases so that there are fewer false positives though (e.g. where the metatype variable is defined locally in the same scope as it is used).

I don't think this comes up often enough to be worth disabling the rule by default, but I'm open to doing that if there are many more reports. I'll leave the ticket open for now.

Was this page helpful?
0 / 5 - 0 ratings