Skip to content

Commit

Permalink
Merge pull request #592 from allevato/synth-init
Browse files Browse the repository at this point in the history
Don't warn about a redundant synthesized memberwise init if it has attributes.
  • Loading branch information
allevato authored Aug 15, 2023
2 parents a27f05e + 00eb59b commit b896e0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Sources/SwiftFormatRules/UseSynthesizedInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
18 changes: 18 additions & 0 deletions Tests/SwiftFormatRulesTests/UseSynthesizedInitializerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit b896e0f

Please sign in to comment.