A private lateinit property is read but never assigned in its file

tx0/kotlin:lateinit-never-assignedrecommendederrorreliability/control-flowdepth fastbug

What it finds

A lateinit property promises to be assigned before it is read. A private one can only be assigned in its own file, so when the file reads it but assigns it nowhere, every read throws UninitializedPropertyAccessException. Usually the assignment was lost in a refactoring, or written to a local variable of the same name.

Properties with annotations, which frameworks fill by reflection (@Inject, @Mock, @BindView and the like), and properties named in a callable reference such as ::name, are left alone. Any assignment to the name in the file counts, whatever the receiver.

From the rule's source, lateinit_never_assigned.rs.

Example from the rule's tests
class Screen {    private lateinit var adapter: Adaptertx0 reports: adapter is read but never assigned, so reading it throws    private lateinit var title: String    private lateinit var unused: String    private lateinit var checked: String    @Inject private lateinit var injected: Service    lateinit var open: String     fun setUp(other: Screen) {        title = "x"        this.checked = "y"    }     fun show() {        adapter.notify()        println(title + injected + open + checked)        if (::checked.isInitialized) println(checked)    }}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:lateinit-never-assigned" = "error"

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-never-assigned] reason="..."

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