kotlin:S1862 Guard conditions in when expression/statement

Language

Kotlin 2.2.0

Rule

kotlin:S1862 This condition duplicates the one on line X.

Why it is a false-positive

In Kotllin 2.2.0 guard conditions were introduced for when expressions and statements:

It seems, that this new feature is not taken into account by the rule above.

Using

  • SonarQube Cloud
  • SonarQube plugin 10.30.0.82084 for IntelliJ IDEA 2025.2025.2 in connected mode SonarQube Cloud

Example

Sonar raises issues on lines 4 and 5. However, the test clearly shows, that the two conditions are not duplicates of line 3.

fun foobar(foo: Boolean, bar: String) : String =
    when (foo) {
        true if bar == "foo" -> "1"
        true if bar == "bar" -> "2"
        true -> "3"
        false -> "4"
    }

class FooTest {
    @org.junit.Test
    fun test() {
        assert(foobar(true, "foo") == "1")
        assert(foobar(true, "bar") == "2")
        assert(foobar(true, "foobar") == "3")
        assert(foobar(false, "abc") == "4")
    }
}

(I could not check if this already has a Jira ticket. The Jira board, which is linked here seems to be private.)

Hi @lnschroeder,

Welcome to the Sonar community and thank you for reporting this FP.

I have created a Jira ticket so that it can be fixed in a future release.
Let us know if you have other feedback regarding the support of Kotlin 2.2

Pierre-Loup