Fix redundantNilInit removing required nil inside ViewBuilder

This commit is contained in:
Nick Lockwood
2023-03-01 19:02:25 +00:00
parent be082afc66
commit 92b4e7df14
2 changed files with 46 additions and 1 deletions
+1 -1
View File
@@ -2852,7 +2852,7 @@ public struct _FormatRules {
formatter.forEach(.keyword("var")) { i, _ in
if formatter.modifiersForDeclaration(at: i, contains: {
$1 == "lazy" || ($1 != "@objc" && $1.hasPrefix("@"))
}) {
}) || formatter.isInResultBuilder(at: i) {
return // Can't remove the init
}
// Check this isn't a Codable
+45
View File
@@ -1772,6 +1772,51 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.redundantNilInit)
}
func testNoRemoveNilInitInViewBuilder() {
let input = """
struct TestView: View {
var body: some View {
var foo: String? = nil
Text(foo ?? "")
}
}
"""
testFormatting(for: input, rule: FormatRules.redundantNilInit)
}
func testNoRemoveNilInitInIfStatementInViewBuilder() {
let input = """
struct TestView: View {
var body: some View {
if true {
var foo: String?
Text(foo ?? "")
} else {
EmptyView()
}
}
}
"""
testFormatting(for: input, rule: FormatRules.redundantNilInit)
}
func testNoRemoveNilInitInSwitchStatementInViewBuilder() {
let input = """
struct TestView: View {
var body: some View {
switch foo {
case .bar:
var foo: String?
Text(foo ?? "")
default:
EmptyView()
}
}
}
"""
testFormatting(for: input, rule: FormatRules.redundantNilInit)
}
// MARK: - redundantLet
func testRemoveRedundantLet() {