Repository: detekt/detekt (kotlin)

Issue:
IgnoredReturnValue should not flag ignored `Nothing`
Rule: `IgnoredReturnValue`
Change: Ignore unhandled return values of type `Nothing`

## Expected Behavior of the rule

As far as I can tell, only `Unit` is never checked by this rule. I propose adding `Nothing` to that list. In practice, it is even less of a normal return value than `Unit` (which is an object an can be assigned to variables), so I can't think of any downsides. As it stands, this is a warning:

```
# detekt.yml
  IgnoredReturnValue:
    active: true
    restrictToConfig: false
    returnValueAnnotations: []
    returnValueTypes: []
    ignoreReturnValueAnnotations: []
    ignoreFunctionCall: []
```

```
fun foo(): Int {
  TODO() // The call TODO is returning a value that is ignored. [IgnoredReturnValue]
}
```

## Context

I think this should avoid false positives for situations were `IgnoredReturnValue` is in heavy use.

This is basically the opposite of #6661 (requiring specific types to _always_ be checked, but which seems to have been implemented already by defining the `returnValueTypes` config property), so maybe there is some overlap on the implementation side as well.

Pull request:
Fixes https://github.com/detekt/detekt/issues/7765

Patch:
diff --git a/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt b/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt
index c76007c2b5d0..66bd2f42ab5f 100644
--- a/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt
+++ b/detekt-rules-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValue.kt
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunctionDescript
 import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
 import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
 import org.jetbrains.kotlin.types.KotlinType
+import org.jetbrains.kotlin.types.typeUtil.isNothing
 import org.jetbrains.kotlin.types.typeUtil.isUnit
 
 /**
@@ -107,6 +108,7 @@ class IgnoredReturnValue(config: Config) : Rule(
 
         val resultingDescriptor = expression.getResolvedCall(bindingContext)?.resultingDescriptor ?: return
         if (resultingDescriptor.returnType?.isUnit() == true) return
+        if (resultingDescriptor.returnType?.isNothing() == true) return
 
         if (ignoreFunctionCall.any { it.match(resultingDescriptor) }) return
 


Test patch:
diff --git a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValueSpec.kt b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValueSpec.kt
index 3cc944d8cdc7..760ee12ec801 100644
--- a/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValueSpec.kt
+++ b/detekt-rules-errorprone/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/IgnoredReturnValueSpec.kt
@@ -1056,6 +1056,28 @@ class IgnoredReturnValueSpec {
             val findings = rule.compileAndLintWithContext(env, code)
             assertThat(findings).isEmpty()
         }
+
+        @Test
+        fun `does not report when a function returns Unit`() {
+            val code = """
+                fun foo() {
+                    println(42)
+                }
+            """.trimIndent()
+            val findings = subject.compileAndLintWithContext(env, code)
+            assertThat(findings).isEmpty()
+        }
+
+        @Test
+        fun `does not report when a function returns Nothing`() {
+            val code = """
+                fun foo() {
+                    TODO("tbd")
+                }
+            """.trimIndent()
+            val findings = subject.compileAndLintWithContext(env, code)
+            assertThat(findings).isEmpty()
+        }
     }
 
     @Nested