Skip to content

Commit

Permalink
Replace restrictToAnnotatedMethods with restrictToConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Jul 31, 2022
1 parent 4ba7064 commit 94a3d4c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion detekt-core/src/main/resources/default-detekt-config.yml
Expand Up @@ -431,7 +431,7 @@ potential-bugs:
active: true
IgnoredReturnValue:
active: true
restrictToAnnotatedMethods: true
restrictToConfig: true
returnValueAnnotations:
- '*.CheckResult'
- '*.CheckReturnValue'
Expand Down
1 change: 1 addition & 0 deletions detekt-core/src/main/resources/deprecation.properties
@@ -1,5 +1,6 @@
complexity>LongParameterList>threshold=Use `functionThreshold` and `constructorThreshold` instead
empty-blocks>EmptyFunctionBlock>ignoreOverriddenFunctions=Use `ignoreOverridden` instead
potential-bugs>IgnoredReturnValue>restrictToAnnotatedMethods=Use `restrictToConfig` instead
potential-bugs>LateinitUsage>excludeAnnotatedProperties=Use `ignoreAnnotated` instead
naming>FunctionParameterNaming>ignoreOverriddenFunctions=Use `ignoreOverridden` instead
naming>MemberNameEqualsClassName>ignoreOverriddenFunction=Use `ignoreOverridden` instead
Expand Down
Expand Up @@ -8,7 +8,9 @@ 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.UnstableApi
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.configWithFallback
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.Configuration
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
Expand Down Expand Up @@ -52,8 +54,14 @@ class IgnoredReturnValue(config: Config = Config.empty) : Rule(config) {
)

@Configuration("if the rule should check only annotated methods")
@Deprecated("Use `restrictToConfig` instead")
private val restrictToAnnotatedMethods: Boolean by config(defaultValue = true)

@Suppress("DEPRECATION")
@OptIn(UnstableApi::class)
@Configuration("If the rule should check only methods matching to configuration, or all methods")
private val restrictToConfig: Boolean by configWithFallback(::restrictToAnnotatedMethods, defaultValue = true)

@Configuration("List of glob patterns to be used as inspection annotation")
private val returnValueAnnotations: List<Regex> by config(listOf("*.CheckResult", "*.CheckReturnValue")) {
it.map(String::simplePatternToRegex)
Expand Down Expand Up @@ -98,7 +106,7 @@ class IgnoredReturnValue(config: Config = Config.empty) : Rule(config) {

val annotations = resultingDescriptor.annotations
if (annotations.any { it in ignoreReturnValueAnnotations }) return
if (restrictToAnnotatedMethods &&
if (restrictToConfig &&
resultingDescriptor.returnType !in returnValueTypes &&
(annotations + resultingDescriptor.containingDeclaration.annotations).none { it in returnValueAnnotations }
) return
Expand Down
Expand Up @@ -771,8 +771,8 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) {
}

@Nested
inner class `restrict to annotated methods config` {
val subject = IgnoredReturnValue(TestConfig(mapOf("restrictToAnnotatedMethods" to false)))
inner class `restrict to config` {
val subject = IgnoredReturnValue(TestConfig(mapOf("restrictToConfig" to false)))

@Test
fun `reports when a function is annotated with a custom annotation`() {
Expand Down Expand Up @@ -811,6 +811,25 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) {
assertThat(findings[0]).hasMessage("The call listOfChecked is returning a value that is ignored.")
}

@Test
fun `reports when a function returns type that should not be ignored`() {
val code = """
import kotlinx.coroutines.flow.MutableStateFlow
fun flowOfChecked(value: String) = MutableStateFlow(value)
fun foo() : Int {
flowOfChecked("hello")
return 42
}
"""
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings)
.singleElement()
.hasSourceLocation(6, 5)
.hasMessage("The call flowOfChecked is returning a value that is ignored.")
}

@Test
fun `does not report when a function has _@CanIgnoreReturnValue_`() {
val code = """
Expand Down Expand Up @@ -849,7 +868,7 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) {
TestConfig(
mapOf(
"ignoreReturnValueAnnotations" to listOf("*.CustomIgnoreReturn"),
"restrictToAnnotatedMethods" to false
"restrictToConfig" to false
)
)
)
Expand All @@ -873,7 +892,7 @@ class IgnoredReturnValueSpec(private val env: KotlinCoreEnvironment) {
TestConfig(
mapOf(
"ignoreFunctionCall" to listOf("foo.listOfChecked"),
"restrictToAnnotatedMethods" to false
"restrictToConfig" to false
)
)
)
Expand Down

0 comments on commit 94a3d4c

Please sign in to comment.