kotlin:S1128 doesn't consider import aliases

Please provide

  • Operating system: Microsoft Windows 10 Pro (10.0.19045 Build 19045)
  • SonarLint plugin version: 9.0.0.75308
  • Programming language you’re coding in: Kotlin
  • Is connected mode used: Not connected
    • Connected to SonarCloud or SonarQube (and which version):

And a thorough description of the problem / question:

I think import advanced.and as playedBy is incorrectly reported as redundant. Note that the import is required for aliasing.

InfixExtensionFunctions.kt

package advanced

data class CacheKey<A, B>(val key: A, val qualifier: B)

infix fun <A, B> A.and(that: B): CacheKey<A, B> = CacheKey(this, that)

ImportAlias.kt

package advanced

import advanced.and as playedBy

fun main()
{
	val cacheKey = "Thomas Anderson" playedBy "Neo"

	println("Key : $cacheKey")
}

Hello @kedarjoshi,

Thanks for the message. There could be a few reasons why you see this issue. Could you please answer these questions:

  • Which Kotlin version you’re using? Is it 1.9?
  • Do you see this issue when analyzing a single file?
  • Does the issue reproduce when you analyze the whole project? (You can do it with sonarlint)
  • Do you see this issue in SonarQube or Sonarcloud, if you’re using them too?
  • Could you please share the analysis log? (you can enable it in sonarlint, it’s not enabled by default)

Best,
Margarita

Which Kotlin version you’re using? Is it 1.9?

Yes, Kotlin 1.9.10.

Do you see this issue when analyzing a single file?

Yes.

Does the issue reproduce when you analyze the whole project? (You can do it with sonarlint)

Yes.

Do you see this issue in SonarQube or Sonarcloud, if you’re using them too?

Not using any of those.

Could you please share the analysis log? (you can enable it in sonarlint, it’s not enabled by default)

Analysing 'ImportAlias.kt'...
Index files
1 file indexed
1 source file to be analyzed
1/1 source file has been analyzed
1 source file to be analyzed
1/1 source file has been analyzed
No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
0 source files to be analyzed
0/0 source files have been analyzed
Found 1 issue and 0 hotspots
1 Like

Thanks for your answer @kedarjoshi,

Unfortunately, I’m still not able to reproduce the issue and Sonar should handle aliases correctly.

I can suggest checking if the issue is present in Kotlin 1.8 and only appears in 1.9 (as support for Kotlin 1.9 was added not so long ago and your SL version might still be missing it.). And could you, please, try the latest SonarLint version, and tell me if you still see the issue?

Also, do you have any additional configuration of your SonarLint plugin? This could also affect the analysis.

Best,
Margarita

Margarita,
I checked with Kotlin 1.8 and 1.9 but the behavior remains same. There is no configuration change either. I also tried with fresh IDE setup and it is still reproducible. I have attached sample project for reference.

kotlin-playground.zip (43.3 KB)

Hi @kedarjoshi,

Thanks for the reproducer. I will investigate it further.

Best,
Margarita

@kedarjoshi thanks again for the reproducer. I was able to find the issue. The thing is that you’re using the same package, and that’s why import is reported as “redundant”, not “unused”. I was missing it and that’s why I couldn’t reproduce the issue.

Yes, in your case import is necessary, because of an alias. So I created a ticket to fix the issue:

https://sonarsource.atlassian.net/browse/SONARKT-366

Thanks for the feedback.

Best,
Margarita

1 Like