diff --git a/CHANGELOG.md b/CHANGELOG.md index ac9824c6a..e81e6ba47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,6 @@ - Add experimental unused import analysis. - The option `--external-encodable-protocols` is deprecated, use `--external-codable-protocols` instead. -- CodingKey enum case are now identified as unused if the corresponding property is also unused in the Codable type. ##### Bug Fixes diff --git a/Sources/PeripheryKit/SourceGraph/Mutators/CodingKeyEnumReferenceBuilder.swift b/Sources/PeripheryKit/SourceGraph/Mutators/CodingKeyEnumReferenceBuilder.swift index 93ac39b75..36ad2b075 100644 --- a/Sources/PeripheryKit/SourceGraph/Mutators/CodingKeyEnumReferenceBuilder.swift +++ b/Sources/PeripheryKit/SourceGraph/Mutators/CodingKeyEnumReferenceBuilder.swift @@ -26,28 +26,21 @@ final class CodingKeyEnumReferenceBuilder: SourceGraphMutator { return [.protocol, .typealias].contains($0.kind) && codableTypes.contains(name) } - guard isCodingKey, isParentCodable else { continue } - - // Build a reference from the Codable type to the CodingKey enum. - for usr in enumDeclaration.usrs { - let newReference = Reference(kind: .enum, usr: usr, location: enumDeclaration.location) - newReference.name = enumDeclaration.name - newReference.parent = parent - graph.add(newReference, from: parent) + guard isCodingKey else { continue } + + // Retain each enum element. + for elem in enumDeclaration.declarations { + guard elem.kind == .enumelement else { continue } + graph.markRetained(elem) } - // For each property in the Codable type, build a reference to its corresponding - // CodingKey enum element. - for decl in parent.declarations { - guard decl.kind == .varInstance, - let enumCase = enumDeclaration.declarations.first(where: { $0.kind == .enumelement && $0.name == decl.name }) - else { continue } - - for usr in enumCase.usrs { - let newReference = Reference(kind: .enumelement, usr: usr, location: decl.location) - newReference.name = enumCase.name - newReference.parent = decl - graph.add(newReference, from: decl) + if isParentCodable { + // Build a reference from the Codable type to the CodingKey enum. + for usr in enumDeclaration.usrs { + let newReference = Reference(kind: .enum, usr: usr, location: enumDeclaration.location) + newReference.name = enumDeclaration.name + newReference.parent = parent + graph.add(newReference, from: parent) } } } diff --git a/Tests/PeripheryTests/RetentionTest.swift b/Tests/PeripheryTests/RetentionTest.swift index d5792aaaf..deab71fb6 100644 --- a/Tests/PeripheryTests/RetentionTest.swift +++ b/Tests/PeripheryTests/RetentionTest.swift @@ -580,7 +580,7 @@ final class RetentionTest: FixtureSourceGraphTestCase { self.assertReferenced(.enum("CodingKeys")) { self.assertReferenced(.enumelement("someUsedVar")) - self.assertNotReferenced(.enumelement("someUnusedVar")) + self.assertReferenced(.enumelement("someUnusedVar")) } } assertReferenced(.struct("FixtureStruct220")) { @@ -589,7 +589,7 @@ final class RetentionTest: FixtureSourceGraphTestCase { self.assertReferenced(.enum("CodingKeys")) { self.assertReferenced(.enumelement("someUsedVar")) - self.assertNotReferenced(.enumelement("someUnusedVar")) + self.assertReferenced(.enumelement("someUnusedVar")) } } }