SonarLint incorrectly claims unncessary import in Kotlin

  • Operating system: Debian Linux 11 (bullseye)
  • SonarLint plugin version:
  • Programming language you’re coding in: Kotlin
  • Is connected mode used: No

SonarLint marks the toList import here as unncessary, even though removing it causes a compilation error:

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.streams.toList

fun repro(): List<Path> {
    val home = Paths.get(System.getenv("HOME"))
    return Files.list(home).toList()

Here it is as a compilable example with reproduction details:

Hello @vocalic,

Thanks for your message. I was able to reproduce the issue both in SonarQube and SonarLint. As far as I understood to run the analysis you use java version equal or higher than 16. In Java 16 method toList() on Stream was introduced, while Kotlin had such extension method since version 1.2.

Unfortunately, even if your maven configuration is correct and uses --release 11, analyzers get method bindings from the runtime version which is 16+ in your case. To fix the issue I created a ticket:

We faced also another similar problem in Java some time ago (when users want to analyze Java 8 code running analysis on Java 11) and the solution was to introduce a special property (more details here)

It was not such a big problem for Kotlin, so for now Kotlin analyzer doesn’t rely on this property. However, it looks like we should add this feature there too.

For now, you can mark this as a false positive.

Here is a ticket for using the property:

Kind regards,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.