https://kotlinlang.org logo
Title
v

Vitali Plagov

03/31/2023, 8:25 AM
Hello. I have this code
private fun assertSomething() {
        var count = 2
        if (count == 0) {
            error("field is empty")
        }
        runCatching {
            doSomeChecks()
        }.onFailure {
            refresh()
            count--
            assertSomething()
        }
    }
And IntelliJ IDEA has an inspection warning on
if (count == 0)
, saying that the condition is always
false
. I think it's a wrong warning, no? The
count
var will be equal to zero if the
doSomeChecks()
will fail twice.
s

Sam

03/31/2023, 8:27 AM
The
count
variable is local to each individual function call. If you call the function again you get a new
count
with a new value.
v

Vitali Plagov

03/31/2023, 8:28 AM
Oh, indeed. Thank you!