Skip to content

False positive on AttributeDynamicLookup subscript #1066

@rnapier

Description

@rnapier

Describe the bug
Periphery incorrectly reports subscript(dynamicMember:) as unused when extending AttributeDynamicLookup to support custom AttributeScope types.

When defining a custom AttributeScope and the corresponding AttributeDynamicLookup subscript (as documented by Apple for extending AttributedString), periphery reports:

warning: Unused function 'subscript(dynamicMember:)'

However, removing this subscript causes compilation to fail because self.myAttribute cannot be resolved on AttributeContainer.

Reproduction

import Foundation

// Custom attribute key
enum MyAttribute: AttributedStringKey {
    typealias Value = String
    static let name = "MyAttribute"
}

// Custom attribute scope - periphery reports this as unused
extension AttributeScopes {
    struct CustomAttributes: AttributeScope {
        let myAttribute: MyAttribute
    }
}

// Dynamic member lookup subscript - periphery reports this as unused
extension AttributeDynamicLookup {
    subscript<T: AttributedStringKey>(
        dynamicMember keyPath: KeyPath<AttributeScopes.CustomAttributes, T>
    ) -> T {
        self[T.self]
    }
}

// Extension that uses the custom attribute via dynamic member lookup
extension AttributeContainer {
    var customValue: String? {
        get { self.myAttribute }  // This requires the subscript and scope above
        set { self.myAttribute = newValue }
    }
}

// Main entry point
@main
struct Main {
    static func main() {
        var container = AttributeContainer()
        container.customValue = "hello"
        print(container.customValue ?? "nil")
    }
}

Output:

/private/tmp/periphery-false-positive/Sources/PeripheryFalsePositive/PeripheryFalsePositive.swift:18:5: warning: Unused function 'subscript(dynamicMember:)'

Environment
Copy the output from running:

3.4.0
swift-driver version: 1.127.14.1 Apple Swift version 6.2.3 (swiftlang-6.2.3.3.21 clang-1700.6.3.2)
Target: arm64-apple-macosx26.0
Xcode 26.2
Build version 17C52

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions