Local variable hides another variable

There is an inspection called “local variable hides member”. I turned this inspection off, because I often use a pattern like this:

class A {
    var stuff: Stuff? = null
    fun doSomething() {
        val stuff = stuff ?: return // quite handy, so I want to allow this
        // ...
    }
}

However, I recently ran into a bug, because a local variable was hidden by another local variable:

fun doSomething() {
    val x = 42
    if (things) {
        if (are) {
            if (nested) {
                val x = 33
                y = y + x // oops, I meant the other x
            }
        }
    }
}

So I wonder if there is an inspection for this? If not, would adding such an inspection be a good idea?

2 Likes