From 82642273436aa82662b6b8050df12af5d21a2f1e Mon Sep 17 00:00:00 2001 From: Ian Leitch Date: Tue, 2 Jan 2024 18:31:00 +0000 Subject: [PATCH] Ignore non-protocol types from redundant protocol conformance replacements --- Sources/PeripheryKit/ScanResultBuilder.swift | 7 ++++--- .../SourceGraph/Mutators/RedundantProtocolMarker.swift | 3 ++- Sources/PeripheryKit/SourceGraph/SourceGraph.swift | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Sources/PeripheryKit/ScanResultBuilder.swift b/Sources/PeripheryKit/ScanResultBuilder.swift index 81ec3b047..1855b8072 100644 --- a/Sources/PeripheryKit/ScanResultBuilder.swift +++ b/Sources/PeripheryKit/ScanResultBuilder.swift @@ -15,9 +15,10 @@ public struct ScanResultBuilder { let annotatedAssignOnlyProperties: [ScanResult] = assignOnlyProperties.map { .init(declaration: $0, annotation: .assignOnlyProperty) } - let annotatedRedundantProtocols: [ScanResult] = redundantProtocols.map { - let inherited = graph.inheritedTypeReferences(of: $0.0).compactMapSet { $0.name } - return .init(declaration: $0.0, annotation: .redundantProtocol(references: $0.1, inherited: inherited)) + let annotatedRedundantProtocols: [ScanResult] = redundantProtocols.map { decl, tuple in + let (references, inherited) = tuple + let inheritedNames = inherited.compactMapSet { $0.name } + return .init(declaration: decl, annotation: .redundantProtocol(references: references, inherited: inheritedNames)) } let annotatedRedundantPublicAccessibility: [ScanResult] = redundantPublicAccessibility.map { .init(declaration: $0.0, annotation: .redundantPublicAccessibility(modules: $0.1)) diff --git a/Sources/PeripheryKit/SourceGraph/Mutators/RedundantProtocolMarker.swift b/Sources/PeripheryKit/SourceGraph/Mutators/RedundantProtocolMarker.swift index e617b1f52..2b0b10fa4 100644 --- a/Sources/PeripheryKit/SourceGraph/Mutators/RedundantProtocolMarker.swift +++ b/Sources/PeripheryKit/SourceGraph/Mutators/RedundantProtocolMarker.swift @@ -58,7 +58,8 @@ final class RedundantProtocolMarker: SourceGraphMutator { if areAllReferencesConformances { // The protocol is redundant. - graph.markRedundantProtocol(protocolDecl, references: protocolReferences) + let inherited = graph.inheritedTypeReferences(of: protocolDecl).filter { $0.kind == .protocol } + graph.markRedundantProtocol(protocolDecl, references: protocolReferences, inherited: inherited) protocolDecl.declarations.forEach { graph.markIgnored($0) } } } diff --git a/Sources/PeripheryKit/SourceGraph/SourceGraph.swift b/Sources/PeripheryKit/SourceGraph/SourceGraph.swift index 147c669cc..8b5006fe2 100644 --- a/Sources/PeripheryKit/SourceGraph/SourceGraph.swift +++ b/Sources/PeripheryKit/SourceGraph/SourceGraph.swift @@ -7,7 +7,7 @@ public final class SourceGraph { private(set) var allDeclarations: Set = [] private(set) var usedDeclarations: Set = [] - private(set) var redundantProtocols: [Declaration: Set] = [:] + private(set) var redundantProtocols: [Declaration: (references: Set, inherited: Set)] = [:] private(set) var rootDeclarations: Set = [] private(set) var redundantPublicAccessibility: [Declaration: Set] = [:] private(set) var rootReferences: Set = [] @@ -65,9 +65,9 @@ public final class SourceGraph { decl.usrs.contains { !allReferencesByUsr[$0, default: []].isEmpty } } - func markRedundantProtocol(_ declaration: Declaration, references: Set) { + func markRedundantProtocol(_ declaration: Declaration, references: Set, inherited: Set) { withLock { - redundantProtocols[declaration] = references + redundantProtocols[declaration] = (references, inherited) } }