mrmans0n / compose-rules

Lint rules for ktlint/detekt aimed to contribute to a healthier usage of Compose. Actively maintained and evolved fork of the Twitter Compose rules.
https://mrmans0n.github.io/compose-rules
Other
550 stars 21 forks source link

Error on multiple pieces of content error when you have an early return #308

Closed joshafeinberg closed 1 month ago

joshafeinberg commented 1 month ago

Not sure if this is just bad code design or if the rule should handle this. This was passing fine on 0.4.5 but failed when I upgraded to 0.4.8

Example Composable:

@Composable
fun EarlyReturnComposable(isVisible: Boolean, modifier: Modifier = Modifier) {
    if (!isVisible) {
        Text("Shouldn't be visible")
        return
    }

    Text("Should be visible")
}

Result:

Composable functions should only be emitting content into the composition from one source at their top level.

See https://mrmans0n.github.io/compose-rules/rules/#do-not-emit-multiple-pieces-of-content for more information. (cannot be auto-corrected)

mrmans0n commented 1 month ago

FWIW I'm not a big fan of early returns in composables, but it has some legit use cases so I've added support to detect these kinds of things. The fix should be present in 0.4.9, which I've just released.