A lateinit property is compared with null, which it never is, as reading it unassigned throws

tx0/kotlin:lateinit-compared-with-nullstrictwarningreliability/null-safetydepth fastbug

What it finds

A lateinit property has a type that admits no null. Before it is assigned, reading it throws UninitializedPropertyAccessException; it never reads as null. So if (adapter == null) does not test whether the property was set: it is always false once the property is set and throws before. ::adapter.isInitialized is the test that was meant.

Reported for == and != with null on a lateinit property of the file, by name or as this.name.

From the rule's source, lateinit_null_check.rs.

Example from the rule's tests
class Screen {    lateinit var adapter: Adapter    var label: String? = null     fun show(other: Screen) {        if (adapter == null) returntx0 reports: adapter is lateinit and never null        if (null != this.adapter) println()tx0 reports: adapter is lateinit        if (label == null) return        if (other.adapter == null) return        if (::adapter.isInitialized) println(adapter)    }     fun local() {        val adapter: Adapter? = find()        if (adapter == null) return    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:lateinit-compared-with-null" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:lateinit-compared-with-null] reason="..."

tx0 explain tx0/kotlin:lateinit-compared-with-null shows the same in the terminal. Found a false positive? Report it.