Using "this" within Kotlin "with" statement / False Positive?

We are facing an issue where Sonar is complaining about unnecessary / useless null checks (kotlin:S6619). As you can see on the small code sample this can indeed be null.

The code might be not ideal for sure…

Is Sonar understanding the this correctly inside Kotlin with statement? Or is Sonar treating this as class reference - as it normally would be?

val financials: FinancialsTO? = null

with(financials) {
    val isReferenceRequired =
        this?.totalAssets?.equals(FinancialTotalAssets.UNDER_20) == true ||
            this?.revenues?.equals(FinancialRevenues.UNDER_40) == true ||
            this?.ownFunds?.equals(FinancialOwnFunds.UNDER_2) == true
    if (this == null)
        throw BusinessException(ClientClassificationValidationErrors.FINANCIALS_NOT_FILLED)

Hello @bego-haddorp,

Thanks for reporting the issue. First, we need to understand which version of which product you’re using. Is it SonarQube Server, SonarQube Cloud, and if it’s Server, what is the version, is it an LTA?

In order to debug the issue properly I need the declaration of the type of the clientClassification as from the code snippet you provided it’s not obvious if financials field of clientClassification can’t be null.

That will really help if you can answer the questions above and provide a minimal reproducer.

Best,
Margarita