Skip to content

Commit

Permalink
Add list of functions to skip in IgnoredReturnValue rule (#4434)
Browse files Browse the repository at this point in the history
* Add list of functions to skip in IgnoredReturnValue rule

* Fix formatting

* Regenerate default detekt config

* Switch to FunctionMatcher, rename property to ignoreFunctionCall

* Update test

* style

* Damn it IJ

* remove testing version

* Update detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt

Co-authored-by: Chao Zhang <zhangchao6865@gmail.com>

Co-authored-by: Brais Gabín <braisgabin@gmail.com>
Co-authored-by: Chao Zhang <zhangchao6865@gmail.com>
  • Loading branch information
3 people committed Jan 28, 2022
1 parent 8a39d2a commit 90ae9ec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions detekt-core/src/main/resources/default-detekt-config.yml
Expand Up @@ -402,6 +402,7 @@ potential-bugs:
- '*.CheckReturnValue'
ignoreReturnValueAnnotations:
- '*.CanIgnoreReturnValue'
ignoreFunctionCall: []
ImplicitDefaultLocale:
active: true
ImplicitUnitReturnType:
Expand Down
1 change: 1 addition & 0 deletions detekt-rules-errorprone/build.gradle.kts
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
compileOnly(projects.detektApi)
implementation(projects.detektTooling)
testImplementation(projects.detektTest)
testImplementation(libs.bundles.testImplementation)
testRuntimeOnly(libs.spek.runner)
Expand Down
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt.rules.bugs

import io.github.detekt.tooling.api.FunctionMatcher
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
Expand Down Expand Up @@ -58,6 +59,17 @@ class IgnoredReturnValue(config: Config = Config.empty) : Rule(config) {
it.map(String::simplePatternToRegex)
}

@Configuration(
"List of function signatures which should be ignored by this rule. " +
"Specifying fully-qualified function signature with name only (i.e. `java.time.LocalDate.now`) will ignore " +
"all function calls matching the name. Specifying fully-qualified function signature with parameters" +
"(i.e. `java.time.LocalDate.now(java.time.Clock)`) will ignore only function calls matching the name " +
"and parameters exactly."
)
private val ignoreFunctionCall: List<FunctionMatcher> by config(emptyList<String>()) {
it.map(FunctionMatcher::fromFunctionSignature)
}

@Suppress("ReturnCount")
override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
Expand All @@ -68,6 +80,8 @@ class IgnoredReturnValue(config: Config = Config.empty) : Rule(config) {
val resultingDescriptor = expression.getResolvedCall(bindingContext)?.resultingDescriptor ?: return
if (resultingDescriptor.returnType?.isUnit() == true) return

if (ignoreFunctionCall.any { it.match(resultingDescriptor) }) return

val annotations = resultingDescriptor.annotations
if (annotations.any { it in ignoreReturnValueAnnotations }) return
if (restrictToAnnotatedMethods &&
Expand Down
Expand Up @@ -813,5 +813,28 @@ object IgnoredReturnValueSpec : Spek({
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}

it("does not report when a function is in ignoreFunctionCall") {
val code = """
package foo
fun listOfChecked(value: String) = listOf(value)
fun foo() : Int {
listOfChecked("hello")
return 42
}
"""
val rule = IgnoredReturnValue(
TestConfig(
mapOf(
"ignoreFunctionCall" to listOf("foo.listOfChecked"),
"restrictToAnnotatedMethods" to false
)
)
)
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}
})

0 comments on commit 90ae9ec

Please sign in to comment.