Skip to content

Commit 871de38

Browse files
authored
Fix unused import false-positives where the only referenced declaration is generated by a macro. Closes #863 (#910)
1 parent 205cdb9 commit 871de38

22 files changed

+121
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
##### Bug Fixes
1212

13-
- None.
13+
- Fix unused import false-positives where the only referenced declaration is generated by a macro.
1414

1515
## 3.1.0 (2025-04-05)
1616

Sources/Indexer/SwiftIndexer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ final class SwiftIndexer: Indexer {
255255
private func establishDeclarationHierarchy() {
256256
graph.withLock {
257257
for (parent, decls) in childDeclsByParentUsr {
258-
guard let parentDecl = graph.explicitDeclarationWithoutLock(withUsr: parent) else {
258+
guard let parentDecl = graph.declarationWithoutLock(withUsr: parent) else {
259259
if varParameterUsrs.contains(parent) {
260260
// These declarations are children of a parameter and are redundant.
261261
decls.forEach { graph.removeWithoutLock($0) }
@@ -276,7 +276,7 @@ final class SwiftIndexer: Indexer {
276276
private func associateLatentReferences() {
277277
for (usr, refs) in referencesByUsr {
278278
graph.withLock {
279-
if let decl = graph.explicitDeclarationWithoutLock(withUsr: usr) {
279+
if let decl = graph.declarationWithoutLock(withUsr: usr) {
280280
for ref in refs {
281281
associateUnsafe(ref, with: decl)
282282
}

Sources/SourceGraph/Mutators/ExtensionReferenceBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class ExtensionReferenceBuilder: SourceGraphMutator {
2121
for extensionDeclaration in graph.declarations(ofKind: kind) {
2222
guard let extendedTypeReference = try graph.extendedDeclarationReference(forExtension: extensionDeclaration) else { continue }
2323

24-
guard let extendedDeclaration = graph.explicitDeclaration(withUsr: extendedTypeReference.usr) else {
24+
guard let extendedDeclaration = graph.declaration(withUsr: extendedTypeReference.usr) else {
2525
// This is an extension on an external type and cannot be folded.
2626
graph.markRetained(extensionDeclaration)
2727
continue

Sources/SourceGraph/Mutators/ExternalOverrideRetainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class ExternalOverrideRetainer: SourceGraphMutator {
2929
{
3030
didIdentifyRelatedRef = true
3131

32-
if graph.explicitDeclaration(withUsr: relatedRef.usr) == nil {
32+
if graph.declaration(withUsr: relatedRef.usr) == nil {
3333
// The related decl is external.
3434
graph.markRetained(decl)
3535
}

Sources/SourceGraph/Mutators/ExternalTypeProtocolConformanceReferenceRemover.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class ExternalTypeProtocolConformanceReferenceRemover: SourceGraphMutator
1717
guard try graph.extendedDeclaration(forExtension: extDecl) == nil else { continue }
1818

1919
// Ensure the type is extended by local protocols.
20-
let protocolDecls = extDecl.related.filter { $0.kind == .protocol }.map { graph.explicitDeclaration(withUsr: $0.usr) }
20+
let protocolDecls = extDecl.related.filter { $0.kind == .protocol }.map { graph.declaration(withUsr: $0.usr) }
2121
guard !protocolDecls.isEmpty else { continue }
2222

2323
// Find all related references that may be protocol members.
@@ -26,7 +26,7 @@ final class ExternalTypeProtocolConformanceReferenceRemover: SourceGraphMutator
2626
for relatedRef in relatedRefs {
2727
// Ensure the relatedDecl is a member of a protocol.
2828
guard
29-
let relatedDecl = graph.explicitDeclaration(withUsr: relatedRef.usr),
29+
let relatedDecl = graph.declaration(withUsr: relatedRef.usr),
3030
let parentDecl = relatedDecl.parent,
3131
protocolDecls.contains(parentDecl)
3232
else { continue }

Sources/SourceGraph/Mutators/ProtocolConformanceReferenceBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class ProtocolConformanceReferenceBuilder: SourceGraphMutator {
4444
// Find all superclasses.
4545
let superclassDecls = graph.inheritedTypeReferences(of: conformingClass)
4646
.filter { $0.kind == .class }
47-
.compactMap { graph.explicitDeclaration(withUsr: $0.usr) }
47+
.compactMap { graph.declaration(withUsr: $0.usr) }
4848
.flatMap(\.declarations)
4949

5050
for unimplementedProtoDecl in unimplementedProtoDecls {
@@ -104,7 +104,7 @@ final class ProtocolConformanceReferenceBuilder: SourceGraphMutator {
104104
guard equivalentDeclarationKinds.contains(conformingDeclaration.kind),
105105
conformingDeclaration.name == relatedReference.name else { continue }
106106

107-
if let protocolDeclaration = graph.explicitDeclaration(withUsr: relatedReference.usr) {
107+
if let protocolDeclaration = graph.declaration(withUsr: relatedReference.usr) {
108108
// Invert the related reference such that instead of the conforming declaration
109109
// referencing the declaration within the protocol, the protocol declaration
110110
// now references the conforming declaration.

Sources/SourceGraph/Mutators/RedundantExplicitPublicAccessibilityMarker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ final class RedundantExplicitPublicAccessibilityMarker: SourceGraphMutator {
9191
// the call site arguments with function parameters, as parameters with
9292
// default types can cause misalignment between the positions of the two.
9393
if let functionRef = $0.parent?.references.first(where: { $0.role == .variableInitFunctionCall }),
94-
let functionDecl = graph.explicitDeclaration(withUsr: functionRef.usr),
94+
let functionDecl = graph.declaration(withUsr: functionRef.usr),
9595
functionDecl.hasGenericFunctionReturnedMetatypeParameters
9696
{
9797
return $0.parent

Sources/SourceGraph/Mutators/RedundantProtocolMarker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class RedundantProtocolMarker: SourceGraphMutator {
3434
.references
3535
.lazy
3636
.filter { $0.kind == .extensionProtocol }
37-
.compactMap { self.graph.explicitDeclaration(withUsr: $0.usr) }
37+
.compactMap { self.graph.declaration(withUsr: $0.usr) }
3838
.flatMap(\.declarations)
3939
.allSatisfy { unusedDeclarations.contains($0) }
4040

Sources/SourceGraph/Mutators/UnusedImportMarker.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ final class UnusedImportMarker: SourceGraphMutator {
2828

2929
// Build a mapping of source files and the modules they reference.
3030
for ref in graph.allReferences {
31-
guard let decl = graph.explicitDeclaration(withUsr: ref.usr) else { continue }
31+
guard let decl = graph.declaration(withUsr: ref.usr) else { continue }
32+
3233
// Record directly referenced modules and also identify any modules that extended
3334
// the declaration. These extensions may provide members/conformances that aren't
3435
// referenced directly but which are still required.

Sources/SourceGraph/Mutators/UnusedParameterRetainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class UnusedParameterRetainer: SourceGraphMutator {
2424
for protoFuncDecl in protoFuncDecls {
2525
let relatedFuncDecls = protoFuncDecl.related
2626
.filter(\.kind.isFunctionKind)
27-
.compactMapSet { graph.explicitDeclaration(withUsr: $0.usr) }
27+
.compactMapSet { graph.declaration(withUsr: $0.usr) }
2828
let extFuncDecls = relatedFuncDecls.filter { $0.parent?.kind.isExtensionKind ?? false }
2929
let conformingDecls = relatedFuncDecls.subtracting(extFuncDecls)
3030

0 commit comments

Comments
 (0)