kotlin:S6517 should ignore interface which contains generic method

What language is this for?
kotlin

Which rule?
S6517

Why do you believe it’s a false-positive/false-negative?
In Kotlin, it is not possible to define a functional interface (fun interface) that contains a generic method due to a compiler restriction: “Single abstract member cannot declare type parameters.”

Example:

fun interface DistributedLock {
    fun <R> transaction(body: () -> R): R // Raise compile error
}

class RedisDistributedLock : DistributedLock {
    override fun <R> transaction(body: () -> R): R {
        return myOwnLockMechanism {
            body()    
        }
    }
}

As demonstrated, the above code raises a compile-time error when trying to define a functional interface with a method that has type parameters.

Workaround Limitations:

While there are workarounds, such as moving the type parameter from the method to the interface level, these solutions are not always sufficient. More importantly, in this context, the interface is not intended to be implemented using lambdas. Thus, enforcing this rule is unnecessary and leads to a false-positive report.

Proposed Improvement:

The rule kotlin:S6517 should be updated to check whether the interface contains generic methods. If it does, the rule should not flag the interface as a candidate for conversion into a functional interface, as this would violate the Kotlin language restrictions.

Versions:

  • SonarQube 2.15.0
  • kotlin 1.9.24
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)