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

Use list config for FunctionOnlyReturningConstant>excludedFunctions #5120

Merged
merged 2 commits into from Aug 3, 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
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")))
Comment on lines +75 to +76
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be something utilizing wildcards?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a new test for that.

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