"Two branches in a conditional structure should not have exactly the same implementation" throws false positive in a `when` block

Sonar returns a false positive for “Two branches in a conditional structure should not have exactly the same implementation” when scanning code samples similar to the following:

sealed interface Base
data class A(val value: String) : Base
data class B(val value: String) : Base

fun unwrap(obj: Base): String {
    return when (obj) {
        is A -> obj.value
        is B -> obj.value
    }
}

fun main() {
    println(unwrap(A("foo")))
}

It reports the implementation for the object being A or B as the same, but does not identify that they are semantically different

Hi @geojakes,

Thank you for your report. This is indeed a false positive and I’ve created a ticket to address this.

Cheers,
Sebastian

1 Like