Unused Variable Warning in Lazy Property

In the code below, let x = 42 appropriately gives us an 'x' was never used warning. However the let z = 42 does not. Is there some intention behind this, or is it an oversight in Xcode?

class Foo {
    lazy var y: Int = {
        let z = 42
        return 12
    }()

    func run() {
        let x = 42 // Initialization of immutable value 'x' was never used...
    }
}

Bonus info:

class Foo {
    lazy var y: Int = {
        let z = 42 // No Warning
        return 12
    }()

    var a: Int {
        let z = 42 // Warning
        return 12
    }

    let b: Int = {
        let z = 42 // No Warning
        return 12
    }()

    func run() {
        let x = 42 // Warning goes away, as X is now captured below

        var b: Int {
            let z = 12 // Warning
            return 42
        }

        let c: Int = { // Warning, as c is unused
            let z = 42 // Warning
            return 12
        }()
    }
}

Is there some intention behind this, or is it an oversight in Xcode?

I’d recommend that you file a bug about this. Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

Thanks @eskimo

https://bugs.swift.org/browse/SR-11992