Fix whitespace issue in rewriter of `redundant_optional_initialization` rule (#4795)

This commit is contained in:
Danny Mösch 2023-03-04 12:47:24 +01:00 committed by GitHub
parent cfd9a26e33
commit 128f37a6b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View File

@ -102,6 +102,11 @@
[SimplyDanny](https://github.com/SimplyDanny) [SimplyDanny](https://github.com/SimplyDanny)
[#4599](https://github.com/realm/SwiftLint/issues/4599) [#4599](https://github.com/realm/SwiftLint/issues/4599)
* Fix whitespaces issue in auto-fix of `redundant_optional_initialization`
rule when multiple variable declaration are involved.
[SimplyDanny](https://github.com/SimplyDanny)
[#4794](https://github.com/realm/SwiftLint/issues/4794)
* Stop triggering `strict_fileprivate` rule on symbols implementing a protocol * Stop triggering `strict_fileprivate` rule on symbols implementing a protocol
in the same file. in the same file.
[SimplyDanny](https://github.com/SimplyDanny) [SimplyDanny](https://github.com/SimplyDanny)

View File

@ -96,12 +96,12 @@ struct RedundantOptionalInitializationRule: SwiftSyntaxCorrectableRule, Configur
"""), """),
Example(""" Example("""
func foo() { func foo() {
var myVar: String? = nil var myVar: String? = nil, b: Int
} }
"""): """):
Example(""" Example("""
func foo() { func foo() {
var myVar: String? var myVar: String?, b: Int
} }
""") """)
] ]
@ -165,14 +165,14 @@ private extension RedundantOptionalInitializationRule {
guard violatingBindings.contains(binding) else { guard violatingBindings.contains(binding) else {
return binding return binding
} }
let newBinding = binding.with(\.initializer, nil) let newBinding = binding.with(\.initializer, nil)
if newBinding.accessor != nil {
if newBinding.accessor == nil {
return newBinding.with(\.trailingTrivia, binding.initializer?.trailingTrivia ?? .zero)
} else {
return newBinding return newBinding
} }
if binding.trailingComma != nil {
return newBinding.with(\.typeAnnotation, binding.typeAnnotation?.with(\.trailingTrivia, .zero))
}
return newBinding.with(\.trailingTrivia, binding.initializer?.trailingTrivia ?? .zero)
}) })
return super.visit(node.with(\.bindings, newBindings)) return super.visit(node.with(\.bindings, newBindings))