Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlsoCouldBeApply: fix false positive when all statements are not it-started expressions #5376

Merged
merged 1 commit into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -8,10 +8,9 @@ import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.rules.IT_LITERAL
import io.gitlab.arturbosch.detekt.rules.safeAs
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.KtQualifiedExpression

/**
* Detects when an `also` block contains only `it`-started expressions.
Expand Down Expand Up @@ -47,23 +46,17 @@ class AlsoCouldBeApply(config: Config = Config.empty) : Rule(config) {
Debt.FIVE_MINS
)

@Suppress("ReturnCount")
override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)

if (expression.calleeExpression?.text == "also") {
val alsoExpression = expression.calleeExpression ?: return

val lambda = expression.lambdaArguments.singleOrNull() ?: expression.valueArguments.single()
.collectDescendantsOfType<KtLambdaExpression>()
.single()
val dotQualifiedsInLambda = lambda.collectDescendantsOfType<KtDotQualifiedExpression>()

if (
dotQualifiedsInLambda.isNotEmpty() &&
dotQualifiedsInLambda.all { it.receiverExpression.textMatches(IT_LITERAL) }
) {
report(CodeSmell(issue, Entity.from(alsoExpression), issue.description))
}
val callee = expression.calleeExpression?.takeIf { it.text == "also" } ?: return
val lambda = expression.lambdaArguments.singleOrNull()?.getLambdaExpression()
?: expression.valueArguments.singleOrNull()?.getArgumentExpression()?.safeAs()
?: return
val statements = lambda.bodyExpression?.statements.orEmpty().ifEmpty { return }
if (statements.all { it.safeAs<KtQualifiedExpression>()?.receiverExpression?.text == IT_LITERAL }) {
report(CodeSmell(issue, Entity.from(callee), issue.description))
}
}
}
Expand Up @@ -118,4 +118,36 @@ class AlsoCouldBeApplySpec {
""".trimIndent()
assertThat(subject.compileAndLint(code)).hasSize(1)
}

@Test
fun `does not report when all statements are not 'it'-started expressions`() {
val code = """
fun test(foo: Foo) {
foo.also {
it.bar()
println(it)
it.baz()
}
}

class Foo {
fun bar() {}
fun baz() {}
}
""".trimIndent()
assertThat(subject.compileAndLint(code)).isEmpty()
}

@Test
fun `does not report when no statements`() {
val code = """
fun test(foo: Foo) {
foo.also {
}
}

class Foo
""".trimIndent()
assertThat(subject.compileAndLint(code)).isEmpty()
}
}