Skip to content

Commit

Permalink
Improve IgnoredReturnValue (#7285)
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed May 20, 2024
1 parent 83203f0 commit 2c7db38
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,15 @@ class IgnoredReturnValue(config: Config) : Rule(

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

val annotations = resultingDescriptor.annotations + resultingDescriptor.findPackage().annotations
if (resultingDescriptor.annotations.any { it in ignoreReturnValueAnnotations } ||
resultingDescriptor.findPackage().annotations.any { it in ignoreReturnValueAnnotations }
) {
return
val annotations = buildList {
addAll(resultingDescriptor.annotations)
addAll(resultingDescriptor.findPackage().annotations)
addAll(resultingDescriptor.containingDeclaration.annotations)
}
if (annotations.any { it in ignoreReturnValueAnnotations }) return
if (restrictToConfig &&
resultingDescriptor.returnType !in returnValueTypes &&
annotations.none { it in returnValueAnnotations } &&
resultingDescriptor.containingDeclaration.annotations.none { it in returnValueAnnotations }
annotations.none { it in returnValueAnnotations }
) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,33 @@ class IgnoredReturnValueSpec {
assertThat(findings).isEmpty()
}

@Test
fun `does not report when a function has a custom annotation on parent`() {
val code = """
package foo
annotation class CustomIgnoreReturn
@CustomIgnoreReturn
object Foo {
fun listOfChecked(value: String) = listOf(value)
}
fun foo() : Int {
Foo.listOfChecked("hello")
return 42
}
""".trimIndent()
val rule = IgnoredReturnValue(
TestConfig(
"ignoreReturnValueAnnotations" to listOf("*.CustomIgnoreReturn"),
"restrictToConfig" to false,
)
)
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}

@Test
fun `does not report when a function is in ignoreFunctionCall`() {
val code = """
Expand Down

0 comments on commit 2c7db38

Please sign in to comment.