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

Support proper globbing in ReturnCount #5118

Merged
merged 3 commits into from Aug 1, 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 @@ -7,10 +7,10 @@ import io.gitlab.arturbosch.detekt.api.Entity
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.api.SplitPattern
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
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
Expand Down Expand Up @@ -60,7 +60,7 @@ class ReturnCount(config: Config = Config.empty) : Rule(config) {
private val max: Int by config(2)

@Configuration("define a list of function names to be ignored by this check")
private val excludedFunctions: SplitPattern by config(listOf("equals")) { SplitPattern(it.joinToString(",")) }
private val excludedFunctions: List<Regex> by config(listOf("equals")) { it.map(String::simplePatternToRegex) }

@Configuration("if labeled return statements should be ignored")
private val excludeLabeled: Boolean by config(false)
Expand Down Expand Up @@ -90,7 +90,7 @@ class ReturnCount(config: Config = Config.empty) : Rule(config) {
}
}

private fun shouldBeIgnored(function: KtNamedFunction) = excludedFunctions.contains(function.name)
private fun shouldBeIgnored(function: KtNamedFunction) = function.name in excludedFunctions

private fun countReturnStatements(function: KtNamedFunction): Int {
fun KtReturnExpression.isExcluded(): Boolean = when {
Expand Down Expand Up @@ -122,3 +122,7 @@ class ReturnCount(config: Config = Config.empty) : Rule(config) {
return false
}
}

private operator fun Iterable<Regex>.contains(input: String?): Boolean {
return input != null && any { it.matches(input) }
}
Expand Up @@ -313,7 +313,7 @@ class ReturnCountSpec {
@Nested
inner class `a subset of functions are ignored` {
val code = """
fun test1(x: Int): Int {
fun factorial(x: Int): Int {
when (x) {
5 -> println("x=5")
4 -> return 4
Expand All @@ -322,7 +322,7 @@ class ReturnCountSpec {
return 6
}

fun test2(x: Int): Int {
fun fac(x: Int): Int {
when (x) {
5 -> println("x=5")
4 -> return 4
Expand All @@ -331,7 +331,7 @@ class ReturnCountSpec {
return 6
}

fun test3(x: Int): Int {
fun fansOfFactorial(x: Int): Int {
when (x) {
5 -> println("x=5")
4 -> return 4
Expand All @@ -347,7 +347,20 @@ class ReturnCountSpec {
TestConfig(
mapOf(
MAX to "2",
EXCLUDED_FUNCTIONS to listOf("test1", "test2")
EXCLUDED_FUNCTIONS to listOf("factorial", "fac"),
)
)
).compileAndLint(code)
assertThat(findings).hasSize(1)
}

@Test
fun `should flag none of the ignored functions using globbing`() {
val findings = ReturnCount(
TestConfig(
mapOf(
MAX to "2",
EXCLUDED_FUNCTIONS to listOf("fa*ctorial"),
)
)
).compileAndLint(code)
Expand Down