Kotlin analysis - false positive Unnecessary imports should be removed

  • SonarQube 9.2.4 (build 50792)
  • sonar-kotlin-plugin

When using arithmetic operators on interfaces with BigDecimal methods we have to import compareTo methods like:

ICurrencyValue.compareTo(other: ICurrencyValue) : Int

Unfortunately, Sonar reports such imports as unused and marks it as Code Smell (Unnecessary imports should be removed).

Obviously we can’t remove it, because of “Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun String.compareTo(other: String, ignoreCase: Boolean = …): Int defined in kotlin.text” error in compilation.

Hello Andrzej,

Thanks for the report! Indeed, there have been a couple of issues with this rule recently, these have been fixed with version 2.8.0 of the sonar-kotlin plugin, which will be released with SonarQube 9.3. You can have a look at the corresponding ticket here.

If you can create a reproducer that you don’t mind to be public, you can verify that this issue is fixed in 2.8.0 for you by scanning the reproducer on SonarCloud, which already has the most recent Kotlin plugin deployed.