collectReferenceToMutatedCollections fails on Kotlin intersection type

  • Languages of the repository: Kotlin
  • Gradle Sonar plugin: 5.0.0.4638
  • Android: Java 17 with target 11
  • Error observed
Cannot analyse '[redacted source].kt' with 'KtChecksVisitor': declarationDescriptor is null for constructor = {String & T} with class org.jetbrains.kotlin.types.IntersectionTypeConstructor
java.lang.IllegalArgumentException: declarationDescriptor is null for constructor = {String & T} with class org.jetbrains.kotlin.types.IntersectionTypeConstructor
	at org.jetbrains.kotlin.js.descriptorUtils.DescriptorUtilsKt.getKotlinTypeFqName(descriptorUtils.kt:26)
	at org.sonarsource.kotlin.checks.CollectionShouldBeImmutableCheck.collectReferenceToMutatedCollections(CollectionShouldBeImmutableCheck.kt:156)
  • Steps to reproduce

The problematic code was of the form

interface Foo {}
suspend fun <T:Any> bar(lambda: (Foo) -> T): List<String> {
  val foo = makeFoo()
  return when (val res = lambda(foo)) {
    is String -> listOf(res)
    is List<*> -> res as List<String>
    else -> emptyList()
  }
}
  • Workaround

None (aside from working out which code is triggering the exception and fixing it :grin:)

Getting a very similar issue (also probably related to casting) at org.sonarsource.kotlin.api.checks.ApiExtensionsKt.expressionTypeFqn(ApiExtensions.kt:334)