Skip to content

Commit

Permalink
Fix false negative MultilineLambdaItParameter on complex multiline si…
Browse files Browse the repository at this point in the history
…ngle statement (#5397)
  • Loading branch information
t-kameyama committed Oct 9, 2022
1 parent 561d25b commit 965e681
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Expand Up @@ -11,7 +11,9 @@ import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import io.gitlab.arturbosch.detekt.rules.IT_LITERAL
import io.gitlab.arturbosch.detekt.rules.hasImplicitParameterReference
import io.gitlab.arturbosch.detekt.rules.implicitParameter
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType

/**
* Lambda expressions are very useful in a lot of cases, and they often include very small chunks of
Expand Down Expand Up @@ -74,8 +76,8 @@ class MultilineLambdaItParameter(val config: Config) : Rule(config) {

override fun visitLambdaExpression(lambdaExpression: KtLambdaExpression) {
super.visitLambdaExpression(lambdaExpression)
val size = lambdaExpression.bodyExpression?.statements?.size
if (size == null || size <= 1) return
val size = lambdaExpression.collectDescendantsOfType<KtBlockExpression>().flatMap { it.statements }.size
if (size <= 1) return

val parameterNames = lambdaExpression.valueParameters.map { it.name }
// Explicit `it`
Expand Down
Expand Up @@ -114,6 +114,21 @@ class MultilineLambdaItParameterSpec(val env: KotlinCoreEnvironment) {
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}

@Test
fun `reports when statement is spanning multiple lines`() {
val code = """
fun f() {
val digits = 1234.let {
check(it > 0) {
println(it)
}
}
}
""".trimIndent()
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}
}

@Nested
Expand Down

0 comments on commit 965e681

Please sign in to comment.