nicklockwood / SwiftFormat

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

SwiftFormat creating incorrect SwiftData code: "A default value requires a fully qualified domain named value" #1649

Closed glacials closed 2 months ago

glacials commented 3 months ago

Thanks for your hard work on SwiftFormat! This issue is not urgent and feel free to wontfix, but wanted to get it logged.

Issue

I have this code:

import Foundation
import SwiftData

@Model
class FooBar {
  var happenedAt: Date = Date.now
  init() {}
}

Running SwiftFormat makes this change:

 import Foundation
 import SwiftData

 @Model
 class FooBar {
-  var happenedAt: Date = Date.now
+  var happenedAt: Date = .now
   init() {}
 }

which yields this error on the affected line:

A default value requires a fully qualified domain named value

This only happens on @Model classes.

Workaround

To work around the issue, I can remove the type hint and put the fully qualified domain named value back in, which makes SwiftFormat leave it alone:

 import Foundation
 import SwiftData

 @Model
 class FooBar {
-  var happenedAt: Date = Date.now
+  var happenedAt = Date.now
   init() {}
 }

Thanks for reading! SwiftFormat is the best.

nicklockwood commented 3 months ago

@glacials thanks for flagging this, I'll get it fixed. The rule causing the issue is redundantType.

You can use the option --redundanttype inferred to avoid this problem for now as it basically does the same thing as your workaround.

nicklockwood commented 2 months ago

@glacials fixed in 0.53.6

glacials commented 2 months ago

Thank you!!