S1128 FP with 'by' and 'collectAsState()'

  • What language is this for? Kotlin
  • Which rule? S1128
  • Why do you believe it’s a false-positive/false-negative? The import of androidx.compose.runtime.getValue is identified as unused by sonarlint but it is not. It is needed if collectAsState is used with by operator instead of using direct assignment of collectAsState().value where it is indeed not needed
  • Are you using
    • SonarCloud?
    • SonarQube - which version?
    • SonarLint - which IDE/version? ANDROID STUDIO FLAMINGO 2022.2.1, SonarLint SonarSource
      • in connected mode with SonarQube or SonarCloud? NO
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import kotlinx.coroutines.flow.StateFlow

fun testFP(someFlow : StateFlow<Int>) {
    val state by someFlow.collectAsState()

    //TODO use state

Hi @KlitosG ,

Thanks for the report. There is good news for you, [this problem has been resolved] recently([SONARKT-307] - Jira).

So you should see the change soon in the next update of SonarLint for Intellij or you can see it today if you use SonarLint in connected mode with SonarCloud.



Thanks Dorian,

It’s not a big deal for me, happy that it is fixed, I will wait for the next update

1 Like

I am now in version of SonarLint and this false positive still persists.
Can you please let me know what version this is fixed on?