Skip to content

Commit

Permalink
Support proper globbing in ReturnCount (#5118)
Browse files Browse the repository at this point in the history
* Support proper globbing in ReturnCount

* Improve tests naming

* Update detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/ReturnCount.kt

Co-authored-by: marschwar <marschwar@users.noreply.github.com>

Co-authored-by: marschwar <marschwar@users.noreply.github.com>
  • Loading branch information
BraisGabin and marschwar committed Aug 1, 2022
1 parent 9acad77 commit 090b5bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
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

0 comments on commit 090b5bc

Please sign in to comment.