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

Fix ReturnCount false positive when excludeReturnFromLambda is enabled #5459

Merged
merged 6 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -13,8 +13,7 @@ import io.gitlab.arturbosch.detekt.api.internal.Configuration
import io.gitlab.arturbosch.detekt.api.simplePatternToRegex
import io.gitlab.arturbosch.detekt.rules.parentsOfTypeUntil
import io.gitlab.arturbosch.detekt.rules.yieldStatementsSkippingGuardClauses
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtReturnExpression
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
Expand Down Expand Up @@ -65,7 +64,7 @@ class ReturnCount(config: Config = Config.empty) : Rule(config) {
@Configuration("if labeled return statements should be ignored")
private val excludeLabeled: Boolean by config(false)

@Configuration("if labeled return from a lambda should be ignored")
@Configuration("if labeled return from a lambda should be ignored (takes precedence over excludeLabeled.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Configuration("if labeled return from a lambda should be ignored (takes precedence over excludeLabeled.")
@Configuration("if labeled return from a lambda should be ignored (takes precedence over excludeLabeled).")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, the configurations for excluding seem to be or conditions. (The added explanation is only making more confusion for the user). So I revert it.

1b806d8

private val excludeReturnFromLambda: Boolean by config(true)

@Configuration("if true guard clauses at the beginning of a method should be ignored")
Expand Down Expand Up @@ -94,8 +93,8 @@ class ReturnCount(config: Config = Config.empty) : Rule(config) {

private fun countReturnStatements(function: KtNamedFunction): Int {
fun KtReturnExpression.isExcluded(): Boolean = when {
excludeLabeled && labeledExpression != null -> true
excludeReturnFromLambda && isNamedReturnFromLambda() -> true
excludeLabeled && labeledExpression != null -> true
else -> false
}

Expand All @@ -113,11 +112,9 @@ class ReturnCount(config: Config = Config.empty) : Rule(config) {
private fun KtReturnExpression.isNamedReturnFromLambda(): Boolean {
val label = this.labeledExpression
if (label != null) {
return this.parentsOfTypeUntil<KtCallExpression, KtNamedFunction>()
.map { it.calleeExpression }
.filterIsInstance<KtNameReferenceExpression>()
.map { it.text }
.any { it in label.text }
return this.parentsOfTypeUntil<KtLambdaExpression, KtNamedFunction>()
.none()
.not()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.none().not() is sort of confusing here. Can we rewrite with any or count() >= 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.count() will unnecessarily materialize the sequence. .any() returns a Boolean and will only materialize at most one element of the sequence.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be isNotEmpty?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a Sequence, isNotEmpty is only on Collections, because for a "sequence" emptiness doesn't mathematically make sense: "Unlike collections, sequences don't contain elements, they produce them."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
return false
}
Expand Down
Expand Up @@ -512,4 +512,42 @@ class ReturnCountSpec {
assertThat(findings).isEmpty()
}
}

@Nested
inner class `function with lambda which has explicit label` {
val code = """
fun test() {
list(1, 2, 3, 4, 5).flatMap lit@{
if (it == 3) return@lit
if (it == 4) return@lit
}
return
}
""".trimIndent()

@Test
fun `should count labeled return of lambda with explicit label`() {
val findings = ReturnCount(TestConfig(mapOf(EXCLUDE_RETURN_FROM_LAMBDA to "false"))).compileAndLint(code)
assertThat(findings).hasSize(1)
}

@Test
fun `should not count labeled return of lambda with explicit label when deactivated by default`() {
val findings = ReturnCount().compileAndLint(code)
assertThat(findings).isEmpty()
}

@Test
fun `excludeReturnFromLambda should take precedence over excludeLabeled`() {
val findings = ReturnCount(
TestConfig(
mapOf(
EXCLUDE_RETURN_FROM_LAMBDA to "true",
EXCLUDE_LABELED to "false"
)
)
).compileAndLint(code)
assertThat(findings).isEmpty()
}
}
}