Skip to content

Commit

Permalink
Fix false positive in LambdaParameterInRestartableEffect (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmans0n authored Aug 30, 2024
1 parent 6d88d11 commit 215847a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtIfExpression
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.psi.psiUtil.isDotSelector

class LambdaParameterInRestartableEffect : ComposeKtVisitor {
override fun visitFile(file: KtFile, emitter: Emitter, config: ComposeKtConfig) {
Expand Down Expand Up @@ -48,6 +49,9 @@ class LambdaParameterInRestartableEffect : ComposeKtVisitor {
?: return@flatMap emptySequence()

val callExpressions = body.findChildrenByClass<KtCallExpression>()
// Filter out dot receivers e.g. `something.myLambda()`
.filterNot { it.isDotSelector() }

val isDisposableEffect = effect.calleeExpression?.text == "DisposableEffect"

// Lambdas used directly: myLambda()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ class LambdaParameterInRestartableEffectCheckTest {
assertThat(errors).isEmpty()
}

@Test
fun `passes when the a dot reference uses the same name as the lambda parameter`() {
@Language("kotlin")
val code =
"""
@Composable
fun Something(onClick: () -> Unit) {
LaunchedEffect(Unit) {
viewModel.onClick()
}
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).isEmpty()
}

@Test
fun `error out when detecting a lambda named onDispose used in a non-DisposableEffect`() {
@Language("kotlin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ class LambdaParameterInRestartableEffectCheckTest {
ruleAssertThat(code).hasNoLintViolations()
}

@Test
fun `passes when the a dot reference uses the same name as the lambda parameter`() {
@Language("kotlin")
val code =
"""
@Composable
fun Something(onClick: () -> Unit) {
LaunchedEffect(Unit) {
viewModel.onClick()
}
}
""".trimIndent()
ruleAssertThat(code).hasNoLintViolations()
}

@Test
fun `error out when detecting a lambda named onDispose used in a non-DisposableEffect`() {
@Language("kotlin")
Expand Down

0 comments on commit 215847a

Please sign in to comment.