diff --git a/rules/common/src/main/kotlin/io/nlopez/compose/rules/LambdaParameterInRestartableEffect.kt b/rules/common/src/main/kotlin/io/nlopez/compose/rules/LambdaParameterInRestartableEffect.kt index bb39a997..b5783abf 100644 --- a/rules/common/src/main/kotlin/io/nlopez/compose/rules/LambdaParameterInRestartableEffect.kt +++ b/rules/common/src/main/kotlin/io/nlopez/compose/rules/LambdaParameterInRestartableEffect.kt @@ -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) { @@ -48,6 +49,9 @@ class LambdaParameterInRestartableEffect : ComposeKtVisitor { ?: return@flatMap emptySequence() val callExpressions = body.findChildrenByClass() + // Filter out dot receivers e.g. `something.myLambda()` + .filterNot { it.isDotSelector() } + val isDisposableEffect = effect.calleeExpression?.text == "DisposableEffect" // Lambdas used directly: myLambda() diff --git a/rules/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/LambdaParameterInRestartableEffectCheckTest.kt b/rules/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/LambdaParameterInRestartableEffectCheckTest.kt index a2c5ff69..d298128f 100644 --- a/rules/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/LambdaParameterInRestartableEffectCheckTest.kt +++ b/rules/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/LambdaParameterInRestartableEffectCheckTest.kt @@ -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") diff --git a/rules/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/LambdaParameterInRestartableEffectCheckTest.kt b/rules/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/LambdaParameterInRestartableEffectCheckTest.kt index e356713e..ea9c2626 100644 --- a/rules/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/LambdaParameterInRestartableEffectCheckTest.kt +++ b/rules/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/LambdaParameterInRestartableEffectCheckTest.kt @@ -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")