From 00eb59b20c69f6ed8912ed905798f91d8e27297a Mon Sep 17 00:00:00 2001 From: Tony Allevato Date: Mon, 14 Aug 2023 16:26:09 -0400 Subject: [PATCH] Don't warn about a redundant synthesized memberwise init if it has attributes. Fixes #591. --- .../UseSynthesizedInitializer.swift | 18 +++++++++++------- .../UseSynthesizedInitializerTests.swift | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Sources/SwiftFormatRules/UseSynthesizedInitializer.swift b/Sources/SwiftFormatRules/UseSynthesizedInitializer.swift index d7bede3a..097328bf 100644 --- a/Sources/SwiftFormatRules/UseSynthesizedInitializer.swift +++ b/Sources/SwiftFormatRules/UseSynthesizedInitializer.swift @@ -46,17 +46,21 @@ public final class UseSynthesizedInitializer: SyntaxLintRule { var extraneousInitializers = [InitializerDeclSyntax]() for initializer in initializers { guard + // Attributes signify intent that isn't automatically synthesized by the compiler. + initializer.attributes.isEmpty, matchesPropertyList( parameters: initializer.signature.parameterClause.parameters, - properties: storedProperties) - else { continue } - guard + properties: storedProperties), matchesAssignmentBody( variables: storedProperties, - initBody: initializer.body) - else { continue } - guard matchesAccessLevel(modifiers: initializer.modifiers, properties: storedProperties) - else { continue } + initBody: initializer.body), + matchesAccessLevel( + modifiers: initializer.modifiers, + properties: storedProperties) + else { + continue + } + extraneousInitializers.append(initializer) } diff --git a/Tests/SwiftFormatRulesTests/UseSynthesizedInitializerTests.swift b/Tests/SwiftFormatRulesTests/UseSynthesizedInitializerTests.swift index 3dbee050..f10a72e7 100644 --- a/Tests/SwiftFormatRulesTests/UseSynthesizedInitializerTests.swift +++ b/Tests/SwiftFormatRulesTests/UseSynthesizedInitializerTests.swift @@ -404,4 +404,22 @@ final class UseSynthesizedInitializerTests: LintOrFormatRuleTestCase { XCTAssertDiagnosed(.removeRedundantInitializer, line: 15) XCTAssertDiagnosed(.removeRedundantInitializer, line: 25) } + + func testMemberwiseInitializerWithAttributeIsNotDiagnosed() { + let input = + """ + public struct Person { + let phoneNumber: String + let address: String + + @inlinable init(phoneNumber: String, address: String) { + self.address = address + self.phoneNumber = phoneNumber + } + } + """ + + performLint(UseSynthesizedInitializer.self, input: input) + XCTAssertNotDiagnosed(.removeRedundantInitializer) + } }