Skip to content

Commit

Permalink
Fix for realm#5075
Browse files Browse the repository at this point in the history
  • Loading branch information
mildm8nnered committed Jun 22, 2023
1 parent 76b9b61 commit 41280b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private extension StructDeclSyntax {

return initializers.filter {
self.initializerParameters($0.parameterList, match: storedProperties) &&
($0.parameterList.isEmpty || initializerBody($0.body, matches: storedProperties)) &&
(($0.parameterList.isEmpty && hasNoSideEffects($0.body)) || initializerBody($0.body, matches: storedProperties)) &&
initializerModifiers($0.modifiers, match: storedProperties) && !$0.isInlinable
}
}
Expand Down Expand Up @@ -215,6 +215,14 @@ private extension StructDeclSyntax {
return statements.isEmpty
}

private func hasNoSideEffects(_ initializerBody: CodeBlockSyntax?) -> Bool {
guard let initializerBody else {
return true
}
return initializerBody.statements.isEmpty
}


// Does the actual access level of an initializer match the access level of the synthesized
// memberwise initializer?
private func initializerModifiers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ enum UnneededSynthesizedInitializerRuleExamples {
self.bar = bar
}
}
"""),
Example("""
struct Foo {
init() {
print("perform side effect")
}
}
"""),
Example("""
struct Foo {
var bar: Int = 0
init(bar: Int = 0) {
self.bar = bar
print("perform side effect")
}
}
""")
]

Expand Down

0 comments on commit 41280b3

Please sign in to comment.