Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive: 'init(unsafeData:)' is unused #646

Closed
RobinFarmer opened this issue Aug 24, 2023 · 1 comment
Closed

False positive: 'init(unsafeData:)' is unused #646

RobinFarmer opened this issue Aug 24, 2023 · 1 comment

Comments

@RobinFarmer
Copy link

I have the following extension:

extension Array {
    init?(unsafeData: Data) {
        guard unsafeData.count % MemoryLayout<Element>.stride == 0 else { return nil }
        self = unsafeData.withUnsafeBytes { .init($0.bindMemory(to: Element.self)) }
    }
}

Which I use like this:

let bytes = [UInt8](unsafeData: byteData)!

however it's being reported as unused.

@ileitch
Copy link
Contributor

ileitch commented Aug 27, 2023

Unfortunately this is a known bug in Swift (swiftlang/swift#56165). You can workaround it by changing the call site to:

let bytes: [UInt8] = Array(unsafeData: byteData)!

@ileitch ileitch closed this as completed Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants