Search code examples
kotlininheritanceintellij-ideakotlin-lateinit

"lateinit var overrides lateinit var"


Let's say I have a setup like so:

abstract class FooTest {

    open lateinit var softAssertions: SoftAssertions

    ... reusable auxiliary functions ...
}
@ExtendWith(SoftAssertionsExtension::class)
class BarTest: FooTest(){

    @InjectSoftAssertions
    override lateinit var softAssertions: SoftAssertions

    ... actual test cases ...
    
}

IntelliJ gives me a very helpful warning for softAssertions in BarTest:

lateinit var overrides lateinit var

Yeeeeeeees ..? Go on, what's your point?

I eventually realised that my lateinit var in FooTest really should be an abstract val, instead - which got rid of the warning - but even so, I'm wondering ... is there something about a lateinit var overriding a lateinit var that I should know but don't and IntelliJ wants to tell me but doesn't?


Solution

  • The reason is described in the issue which added the inspection:

    It seems suspicious that an instance of A2 [BarTest in your case] will have two fields for the single property and the one from A1 [FooTest] remains effectively unused.

    ...

    why do we consider suspicious just a case with lateinit properties? I'd say here that any case where one property with backing field overrides another property with backing field is suspicious.

    Properties with backing fields in general can have custom setters, so overriding them can make sense in some situations. On the contrary, lateinit properties never have custom setters, thus overriding them makes the overridden property backing fields always unused.

    These two different backing fields also can lead to fun things like 'isInitialized' property of a lateinit variable is false when it is overridden as a var by another class.

    It could make sense to add an exception where overriding property has an annotation like in your case.