kotlin:S1481 false-positive unused variable when assigned to hashmap

Hi,

we face an issue with the rule Unused local variables should be removed.

Here are infos for this issue:

  • Language: Kotlin
  • Rule: kotlin:S1481
  • Why is it an false-positive?: a Variable which get’s assigned to a hashmap is reported as unused variable in Sonar.
  • SonarQube Version 9.9.1 (LTS - developer edition)
  • SonarLint (connected to the SonarQube instance) in version 8.4.0.73538 does not report this as issue

Reproducable example snippet:

fun unusedVarFalsePositive() {
    val value = "not-unused"
    val map = hashMapOf<String, String>()
    map["used"] = value
}

Reported as:
image

Thanks for your help
Michael

1 Like

Hello @Michael_Berner,

Thanks for your message. Unfortunately, I’m not able to reproduce your issue.
And I think I know why. This rule actually relies on the compiler warnings and we had a bunch of problems with this in the past.

The reason why this error is happening is most likely in the incomplete semantic model. So in order to check it I need a little bit more input from your side:

  • logs

  • sonar analysis configuration

  • Are you using Gradle or Maven?

Also, your problem might be similar to this thread: "Unused local variables should be removed" is broken - #8 by Margarita_Nedzelska

And we also have a ticket in the backlog that might help to improve this rule:
https://sonarsource.atlassian.net/browse/SONARKT-269

Looking forward to your answer!

Regards,
Margarita

1 Like

Hello Margarita,

thanks for the quick response.

Here is an example project which showcases the error.

The issue comes with the kotlin version 1.9.0, but not with 1.8.22

I documented the test execution in the README.md, hope this helps.

Regards
Michael

1 Like

Thanks for the reproducer @Michael_Berner,

I will have a look there. Actually, we haven’t updated Kotlin compiler dependency to 1.9 as it was released recently, but the latest SonarQube was released a few months before. We’re currently working on that and indeed this could affect the results.

Regards,
Margarita

1 Like

After trying your reproducer I can confirm that I can’t reproduce this with the SQ version of 10.1 and I can’t reproduce it with the master build of our Kotlin analyzer.

I was able to reproduce the issue on LTS 9.9.

This happened due to the fact that In the LTS 9.9 version of SQ, the latest officially supported Kotlin version was 1.7. I think it is because Kotlin 1.8 appeared at a time when we already had a code freeze before LTS. We added support for Kotlin 1.8 later in the SQ 10. And we’re going to add support for Kotlin 1.9 soon.

This doesn’t mean that you can’t analyze your project with Kotlin 1.8 or 1.9 with the SQ LTS 9.9. In most cases, it works fine, unless new language features were introduced. However, there could be changes in the compiler that might lead to such FPs or FNs that you can observe in your case.

If you need to always have support for the latest languages versions, you should maybe consider switching to the latest SQ or SC (SonarCloud).

Regards,
Margarita

1 Like

Hi Margarita ,

thanks for your test and explanation which makes sence :slight_smile:.

Regards
Michael

1 Like

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