Skip to content

Commit

Permalink
Use list config for FunctionOnlyReturningConstant>excludedFunctions (
Browse files Browse the repository at this point in the history
…#5120)

* Use list config for FunctionOnlyReturningConstant>excludedFunctions

* Update detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/FunctionOnlyReturningConstant.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 3, 2022
1 parent 3bc81d2 commit f37d44b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion detekt-core/src/main/resources/default-detekt-config.yml
Expand Up @@ -556,7 +556,7 @@ style:
active: true
ignoreOverridableFunction: true
ignoreActualFunction: true
excludedFunctions: ''
excludedFunctions: []
LibraryCodeMustSpecifyReturnType:
active: true
excludes: ['**']
Expand Down
Expand Up @@ -8,10 +8,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.isActual
import io.gitlab.arturbosch.detekt.rules.isOpen
import io.gitlab.arturbosch.detekt.rules.isOverride
Expand Down Expand Up @@ -52,7 +52,7 @@ class FunctionOnlyReturningConstant(config: Config = Config.empty) : Rule(config
private val ignoreActualFunction: Boolean by config(true)

@Configuration("excluded functions")
private val excludedFunctions: SplitPattern by config("") { SplitPattern(it) }
private val excludedFunctions: List<Regex> by config(emptyList<String>()) { it.map(String::simplePatternToRegex) }

@Configuration("allows to provide a list of annotations that disable this check")
@Deprecated("Use `ignoreAnnotated` instead")
Expand Down Expand Up @@ -110,7 +110,7 @@ class FunctionOnlyReturningConstant(config: Config = Config.empty) : Rule(config
}

private fun isNotExcluded(function: KtNamedFunction) =
!excludedFunctions.contains(function.name) && !annotationExcluder.shouldExclude(function.annotationEntries)
function.name !in excludedFunctions && !annotationExcluder.shouldExclude(function.annotationEntries)

private fun isReturningAConstant(function: KtNamedFunction) =
isConstantExpression(function.bodyExpression) || returnsConstant(function)
Expand All @@ -127,3 +127,7 @@ class FunctionOnlyReturningConstant(config: Config = Config.empty) : Rule(config
return isConstantExpression(returnExpression?.returnedExpression)
}
}

private operator fun Iterable<Regex>.contains(input: String?): Boolean {
return input != null && any { it.matches(input) }
}
Expand Up @@ -63,13 +63,29 @@ class FunctionOnlyReturningConstantSpec {
}

@Test
fun `does not report excluded function which returns a constant`() {
fun `does not report excluded function which returns a constant (with string configuration)`() {
val code = "fun f() = 1"
val config = TestConfig(mapOf(EXCLUDED_FUNCTIONS to "f"))
val rule = FunctionOnlyReturningConstant(config)
assertThat(rule.compileAndLint(code)).isEmpty()
}

@Test
fun `does not report excluded function which returns a constant`() {
val code = "fun f() = 1"
val config = TestConfig(mapOf(EXCLUDED_FUNCTIONS to listOf("f")))
val rule = FunctionOnlyReturningConstant(config)
assertThat(rule.compileAndLint(code)).isEmpty()
}

@Test
fun `does not report wildcard excluded function which returns a constant`() {
val code = "fun function() = 1"
val config = TestConfig(mapOf(EXCLUDED_FUNCTIONS to listOf("f*ion")))
val rule = FunctionOnlyReturningConstant(config)
assertThat(rule.compileAndLint(code)).isEmpty()
}

@Test
@DisplayName(
"does not report excluded annotated function which returns a constant when given \"kotlin.SinceKotlin\""
Expand Down

0 comments on commit f37d44b

Please sign in to comment.