compose-lints icon indicating copy to clipboard operation
compose-lints copied to clipboard

RememberMissingDetector doesn't detect primitive state creation, issues false positive for `retain` and other lambdas

Open andrewbailey opened this issue 4 months ago • 1 comments

RememberMissingDetector deviates from the UnrememberedStateDetector (link to source) inspection that ships with compose, and seems to have incorrect behaviors.

RememberMissingDetector will not flag this code which is missing calls to remember, but UnrememberedStateDetector will:

@Composable fun Foo() {
    val fooInt = mutableIntStateOf(42)
    val fooLong = mutableLongStateOf(42L)
    val fooFloat = mutableFloatStateOf(42f)
    val fooDouble = mutableDoubleStateOf(42.0)
    val list = mutableStateListOf("A", "B", "C")
}

Similarly, RememberMissingDetector falsely flags this code, which UnrememberedStateDetector won't:

val globalState = mutableMapOf<String, MutableState<Int>>()

@Composable fun Foo() {
    // Retain satisfies remember requirement
    val fooInt = retain { mutableStateOf(42) }
    // This is unusual and likely discouraged by the opinions of the library, but technically valid.
    // Adding a call to `remember` isn't useful for code like this.
    val localState = globalState.getOrPut("Foo") { mutableStateOf(0) }
}

RememberMissingDetector should probably be updated to use the @StateFactoryMarker annotation instead of looking at hardcoded names of states that need to be remembered. It should also consider retain to satisfy the remember requirement, and possibly be less judicious towards lambdas in general. Is this inspection a value add over the one that ships with compose? Is there something we should change in UnrememberedStateDetector instead of effectively duplicating the inspection?

(BTW: retain is not yet API stable. We may move it to a different package.)

andrewbailey avatar Aug 20 '25 16:08 andrewbailey

Note that we also introduced RememberInComposition annotation in Compose 1.9 to flag instances that should be memoized.

ShikaSD avatar Aug 20 '25 17:08 ShikaSD