Incorrect Warning "Kotlin:S1128 unnecessary import"

  • Operating system: Windows 11 Pro / 23H2 / 22631.3593
  • SonarLint plugin version: SonarLint 10.6.2.78685
    (with. Android Studio 2023.3.1 Patch 1 / Build #AI-233.14808.21.2331.11709847)
  • Programming language you’re coding in: Kotlin
  • Kotlin Compiler Version : 1.9.0
  • Language Version & API Version : Latest stable (2.0) (same both)

And a thorough description of the problem / question:

In Kotlin code, Sonarlint plugin warning kotlin:S1128 for the below import:

import androidx.compose.foundation.lazy.items

This import is used in your code as follows:

Surface(modifier = modifier) {
    LazyColumn(
        contentPadding = PaddingValues(horizontal = 8.dp
        modifier = modifier
            .height(350.dp)
    ) {
        items(subMenuCollectionData) { item ->
            Text(modifier = Modifier
                .clickable { onItemSelected(item.text) }
...

sonarlint_log.txt (64.1 KB)

If I remove the import statement import androidx.compose.foundation.lazy.items, the code will not compile because items is an extension function provided by the androidx.compose.foundation.lazy package, and it is necessary for the LazyColumn to work correctly with collections.
I think this issue might be similar to the one described in the following link: False-positive java:S1128 unnecessary import.

1 Like

Same for

import androidx.compose.runtime.getValue

needed for the Delegated properties “by” keyword:

val state by viewModel.viewState.collectAsState()
2 Likes

Same problem here - sonarlint 10.7.0.78874. Unfortunately it seems that this problem has occured before: kotlin:S1128 False Positive Jetpack Compose and the Jira ticket is marked as solved :man_shrugging:

image

image

1 Like

Hi @Remer , @dominikkv and @Rodrigo_de_Moraes_Al . And welcome @dominikkv to the community :slight_smile:

Sadly, I’m unable to reproduce the issue. I’m using SonarLint 10.11. Does the issue still occur for you in this version?

This is my minimal reproducer:

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items

val column = LazyColumn {
    items(listOf(1,2,3)) { _ -> }
}

Observation:

  • S1128 when I comment out the line items(listOf(1,2,3)) { _ -> }
  • Compiler error when I comment out the second import
  • Everything fine when neither of both is commented out.

Cheers,
Marco