False-Positive in kotlin:S1128

hello!

SonarCloud incorrectly reports import com.github.michaelbull.result.Result as an unused import under rule kotlin:S1128, even though the import is used by Kotlin’s type inference and is required for correct compilation and IDE behavior.

Any help appreciated!

Thanks,

Damien

ALM: Github
CI: GitHub Actions
Language: Kotlin
SonarCloud Organization: thriveglobal
Rule: kotlin:S1128 — “Unused imports should be removed”
Scope: False positive
Expected behavior: No issue raised
Actual behavior: SonarCloud flags valid import as unused

Hi,

Welcome to the community!

Could you provide a full reproducer that includes the usage via type inference?

 
Thx,
Ann

@ganncamp We see the same problems, with many different types, probably around our update to Kotlin 2.3. We’re on Community Build v25.9.0.112764.

It complains about unused imports of types in generic definitions or annotations. The “pattern” is that it seems as if it gets Java’s `java.lang.*` and Kotlin’s `kotlin.*` built-in types mixed up that do not require an import, i.e. it complains about

import dagger.Module // probably mixed up with java.lang.Module
import org.koin.core.module.Module // again, probably mixed up with java.lang.Module
import my.app.package.domain.model.Error // probably mixed up with kotlin.Error or java.lang.Error

Right now the rule is creating so many false positives that we likely have to deactivate it.

Hi @Thomas_Keller,

Can you provide a full reproducer that includes the unrecognized usage? Also, are you compiling before analysis?

 
Thx,
Ann

Hi Ann, unfortunately my time is very limited, so I cannot come up with a public reproducer. Sonarqube is executed in a separate Jenkins build step where compilation happened in previous steps already, so I’d say yes, compilation happened before analysis.

I guess it has something to do with compilation skipping, just figured that we had compilation skipping enabled via `systemProp.sonar.gradle.skipCompile=true` in gradle.properties. What is the default now in the Gradle plugin, is compilation still implicitely kicked off when skipCompile=false or non-existant?

Hi,

Are you asking about the sonar.gradle.skipCompile property? It was added to allow you to suppress the scanner’s normal triggering of compilation. So yes, omitting it would allow that compile to be triggered.

 
HTH,
Ann

Ok, I understood the original discussion about this on SonarScanner for Gradle: you can now decide when to compile - #3 by G00fY2 as if the scanner plugin would at some point in time stop doing the compilation altogether and enfore a designated “assemble” step before analysis takes place.

Hi,

Okay, you’re stirring dormant brain cells to life…

Checking the docs, I see this:

All tasks that produce output that should be included in the analysis need to be executed before the sonar task runs. Typically, these are compile tasks, test tasks, and Overview tasks. To meet these needs, the plugin adds a task dependency from sonar on test if the Java plugin is applied. Further task dependencies can be added as needed.

I take that to mean that compile will be triggered by default since test would necessarily require compilation, right? Plus Kotlin analysis needs compilation to be accurate.

But I’m going to flag the experts on this to be sure.

 
Ann

Hello @Thomas_Keller !

Could you please provide more information about how your build is structured? Even if not a full reproducer, it will help us narrow down the problem.

I assume you are building with Gradle and running Sonar with sonar-scanner-gradle? Some additional questions I have:

  • Are you using Android Gradle plugin or Kotlin multiplatform?
  • Are you using custom source sets, like src/integrationTest? Or are you seeing the problem with the regular main source set?
  • Which version of sonar-scanner-gradle are you using?

Thanks in advance