Skip to content

Commit 00eb59b

Browse files
committed
Don't warn about a redundant synthesized memberwise init if it has attributes.
Fixes #591.
1 parent a27f05e commit 00eb59b

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

Sources/SwiftFormatRules/UseSynthesizedInitializer.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,21 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
4646
var extraneousInitializers = [InitializerDeclSyntax]()
4747
for initializer in initializers {
4848
guard
49+
// Attributes signify intent that isn't automatically synthesized by the compiler.
50+
initializer.attributes.isEmpty,
4951
matchesPropertyList(
5052
parameters: initializer.signature.parameterClause.parameters,
51-
properties: storedProperties)
52-
else { continue }
53-
guard
53+
properties: storedProperties),
5454
matchesAssignmentBody(
5555
variables: storedProperties,
56-
initBody: initializer.body)
57-
else { continue }
58-
guard matchesAccessLevel(modifiers: initializer.modifiers, properties: storedProperties)
59-
else { continue }
56+
initBody: initializer.body),
57+
matchesAccessLevel(
58+
modifiers: initializer.modifiers,
59+
properties: storedProperties)
60+
else {
61+
continue
62+
}
63+
6064
extraneousInitializers.append(initializer)
6165
}
6266

Tests/SwiftFormatRulesTests/UseSynthesizedInitializerTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,22 @@ final class UseSynthesizedInitializerTests: LintOrFormatRuleTestCase {
404404
XCTAssertDiagnosed(.removeRedundantInitializer, line: 15)
405405
XCTAssertDiagnosed(.removeRedundantInitializer, line: 25)
406406
}
407+
408+
func testMemberwiseInitializerWithAttributeIsNotDiagnosed() {
409+
let input =
410+
"""
411+
public struct Person {
412+
let phoneNumber: String
413+
let address: String
414+
415+
@inlinable init(phoneNumber: String, address: String) {
416+
self.address = address
417+
self.phoneNumber = phoneNumber
418+
}
419+
}
420+
"""
421+
422+
performLint(UseSynthesizedInitializer.self, input: input)
423+
XCTAssertNotDiagnosed(.removeRedundantInitializer)
424+
}
407425
}

0 commit comments

Comments
 (0)