# Condition is always false - Type check warning with Kotlin 2.0.0, not with Kotlin 1.9.24

**URL:** <https://discuss.kotlinlang.org/t/condition-is-always-false-type-check-warning-with-kotlin-2-0-0-not-with-kotlin-1-9-24/28696>\
**Category:** Language Design\
**Created:** [May 31, 2024, 12:08pm UTC](https://discuss.kotlinlang.org/t/condition-is-always-false-type-check-warning-with-kotlin-2-0-0-not-with-kotlin-1-9-24/28696 "2024-05-31T12:08:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florent.Biville](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/florent.biville/32/10887_2.png) [@Florent.Biville](https://discuss.kotlinlang.org/u/Florent.Biville)\
**Post date:** [May 31, 2024, 12:08pm UTC](https://discuss.kotlinlang.org/t/condition-is-always-false-type-check-warning-with-kotlin-2-0-0-not-with-kotlin-1-9-24/28696/1 "2024-05-31T12:08:23Z")

</div>

Hello,

TL;DR: the reproducer is available [here](https://github.com/fbiville/kotlin-200-bug-questionmark).

We have an extension function of a third-party type that is implemented as follows:

```kotlin
    fun Config.validateNonEmptyIfVisible(name: String) {
        this.configValues()
            .first { it.name() == name }
            .let { config ->
                if (config.visible() &&
                    (when (val value = config.value()) {
                        is Int? -> value == null
                        is Boolean? -> value == null
                        is String? -> value.isNullOrEmpty()
                        is List<*>? -> value.isEmpty()
                        else ->
                            throw IllegalArgumentException(
                                "unexpected value '$value' for configuration $name"
                            )
                    })
                ) {
                    System.err.println("Invalid value for configuration $name: Must not be blank.")
                }
            }
    }

```

The compiler for Kotlin 2.0.0 emits a warning for the `Boolean?` type check and our build is configured to fail on compilation warnings, so we currently cannot upgrade.

The same code compiles fine with Kotlin 1.9.24.

Is this is a compiler bug or is it an issue on our end?

---

<div class="post-metadata">

**Author:** ![alexis.manin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexis.manin/32/8381_2.png) [@alexis.manin](https://discuss.kotlinlang.org/u/alexis.manin)\
**Post date:** [May 31, 2024, 4:35pm UTC](https://discuss.kotlinlang.org/t/condition-is-always-false-type-check-warning-with-kotlin-2-0-0-not-with-kotlin-1-9-24/28696/2 "2024-05-31T16:35:06Z")

</div>

I think that the check for a nullable type confuses the compiler. And to be fair, it also confuses me 🤔.

For me, either the value is null, or it is of a certain type. I do not really understand the reason for `is T?` (and that makes me wonder why the compiler even allow it).

Personally, I would simplify your` when` instruction like that:

```kotlin
when (val value = config.value) {
    null -> true
    is Int, is Boolean -> false
    is String -> value.isEmpty()
    is Collection<*> -> value.isEmpty()
    else -> throw IllegalArgumentException("unexpected value '$value' for configuration $name")
}
```

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [May 31, 2024, 8:15pm UTC](https://discuss.kotlinlang.org/t/condition-is-always-false-type-check-warning-with-kotlin-2-0-0-not-with-kotlin-1-9-24/28696/3 "2024-05-31T20:15:19Z")

</div>

That code confuses me, too.

If the value is null, then it’ll take the first (`Int?`) branch. The second branch will never see a null, so it’s equivalent to `is Boolean` (and so will always return false).

What do you _expect_ the code to be doing?

(In case it’s not already clear, `is` checks the _run-time_ type of `value`, i.e. of the object it refers to or null if it doesn’t; it ignores any static compile-time information about what it _could_ refer to, and so it treats all nulls the same.)

The code is technically valid, even it it might not be doing exactly what you expect, so it shouldn’t generate a compilation error. But Kotlin’s quite eager to give warnings when strictly valid code is likely to be mistaken (such as when a `==` check can’t possibly succeed), and so a compilation warning here is arguably justified. I expect that the new Kotlin 2 compiler is a bit smarter about such cases, and so gives a warning when the older, not-quite-so-smart compiler doesn’t.

I’d do as Alexis suggests, and treat null as a separate case.

(And/or maybe reconsider whether a compilation warning should necessarily be a build failure. There are a few other rare cases where code with a warning might arguably be justified, such as checking for a null value in a variable that Kotlin thinks is non-nullable but you know has come from a Java framework and could be null in rare situations.)

---

<div class="post-metadata">

**Author:** ![Florent.Biville](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/florent.biville/32/10887_2.png) [@Florent.Biville](https://discuss.kotlinlang.org/u/Florent.Biville)\
**Post date:** [June 3, 2024, 9:37am UTC](https://discuss.kotlinlang.org/t/condition-is-always-false-type-check-warning-with-kotlin-2-0-0-not-with-kotlin-1-9-24/28696/4 "2024-06-03T09:37:18Z")

</div>

Thanks everyone, we extract the `null` check in a separate clause, that indeed makes a lot more sense.
