kotlin:S6615 The value assigned here is never used

The variable is instanciated through by rememberSaveable() (note that it does the same through remember(). Restoring its state IS important, as it affects the state that it will have after recomposition.

I think that it is easy to reproduce without my entire repos, but if you want there is a test that fails if I remove this assignation: TestAuthentication#testKeepSessionDialogNotReopens()

1 Like

Hi @Axl-Lvy,

Thank you for this feedback about rule S6615.
My first intuition is that the rule is raising correctly. It’s true that changing the value of a local delegated property such as canShowSignedInDialog may trigger side effects. Currently the rule does not take these side effects into account.

The variable is instanciated through by rememberSaveable() (note that it does the same through remember() . Restoring its state IS important, as it affects the state that it will have after recomposition.

Assigning false to the local property will also trigger a recomposition. In that sense, the following pattern seems fragile:

var canShowStaySignedInDialog by rememberSaveable { mutableStateOf(false) }
//...
 if (canShowStaySignedInDialog) {
    canShowStaySignedInDialog = false
}

States are typically changed when events happen. Here is an example inspired from the Jetpack Compose documentation

@Composable
fun DialogExamples() {
    var openAlertDialog by remember { mutableStateOf(false) }
    if(openAlertDialog) {
        AlertDialogExample(
            onDismissRequest = { openAlertDialog = false },
            onConfirmation = { openAlertDialog = false },
        )
    }
    Button(onClick = { openAlertDialog = true }) { Text("Open Alert Dialog") }
}

In this case S6615 will not trigger as the state is changed in a lambda.

Thanks @Axl-Lvy for reporting this.

This rule is based on the compiler diagnostic and seems to be affected by a compiler bug:
https://youtrack.jetbrains.com/projects/KT/issues/KT-78881/K2-False-positive-Assigned-value-is-never-read-in-composable-function

Looks like the bug is fixed in 2.3, so once the Kotlin 2.3 is released, we’ll upgrade the analyzer and the issue must disappear.

Meanwhile you can ignore the issue or report it as an FP.

Best,
Margarita

1 Like

Sorry @Axl-Lvy,

I might have been too fast in my judgement. The bug I’m referencing is related to the non-inline lambda scope and now I realized you don’t use lambda, so I agree with what @Pierre-Loup_Tristant answered here.

So, in your case the issue will still be reported in 2.3.

However, while we can discuss what is the best practice in Compose in your case, what is not, I don’t think such issue should be raised here as technically “assigned value is used”.

Since, the root cause seems to be in the compiler diagnostics. You can double-check the behavior by enabling extended diagnostics and report this issue to the Kotlin maintainers (https://youtrack.jetbrains.com/projects/KT). I think they’ll be the best people to judge whether this is a bug or the intended behavior due to this comment from the Kotlin issue tracker:

While it is true that setValue of delegated properties can have side effects, we should still mark unread writes as a warning since those side effects are exactly that: side effects, and they should not impact the local analysis of the local property.