Skip to content

Commit

Permalink
Add some debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldickson committed Oct 12, 2024
1 parent e6144d2 commit ef58f0e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
22 changes: 18 additions & 4 deletions src/main/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.agodadev.kraftdetekt

import io.gitlab.arturbosch.detekt.api.*
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.types.typeUtil.isNothing
Expand All @@ -19,9 +20,9 @@ class IgnoredReturnValueRule(config: Config) : Rule(config) {

println("Visiting call expression: ${expression.text}")

// Ignore constructor calls
if (expression.calleeExpression is KtConstructorCalleeExpression) {
println(" Ignoring constructor call")
// Ignore constructor calls and calls within throw expressions
if (expression.calleeExpression is KtConstructorCalleeExpression || isWithinThrowExpression(expression)) {
println(" Ignoring constructor call or throw expression")
return
}

Expand Down Expand Up @@ -56,7 +57,9 @@ class IgnoredReturnValueRule(config: Config) : Rule(config) {
parent is KtDotQualifiedExpression -> println(" Parent is dot qualified expression")
parent is KtBlockExpression -> {
println(" Parent is block expression")
if (parent.statements.lastOrNull() != expression) {
val isLastStatement = parent.statements.lastOrNull() == expression
val isOnlyStatement = parent.statements.size == 1
if (!isLastStatement && !isOnlyStatement) {
println(" Reporting issue: ignored return value in block")
report(CodeSmell(
issue,
Expand All @@ -78,4 +81,15 @@ class IgnoredReturnValueRule(config: Config) : Rule(config) {
println(" Not reporting: return type is Unit or Nothing")
}
}

private fun isWithinThrowExpression(expression: KtExpression): Boolean {
var current: PsiElement? = expression
while (current != null && current !is KtFunction) {
if (current is KtThrowExpression) {
return true
}
current = current.parent
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IgnoredReturnValueRuleTest {
val x = returnsString()
if (returnsInt() > 0) {}
}
""".trimIndent()
""".trimIndent()

val findings = IgnoredReturnValueRule(Config.empty).compileAndLintWithContext(env, code)

Expand All @@ -42,12 +42,15 @@ class IgnoredReturnValueRuleTest {
}

assertThat(findings).hasSize(2)
assertThat(findings[0].message).isEqualTo("The return value of this function call is ignored.")
assertThat(findings[1].message).isEqualTo("The return value of this function call is ignored.")
assertThat(findings.map { it.entity.signature }).containsExactlyInAnyOrder(
"Test.kt\$returnsString()",
"Test.kt\$returnsInt()"
)
findings.forEach { finding ->
assertThat(finding.message).isEqualTo("The return value of this function call is ignored.")
}
}

// Update other test methods similarly...

@Test
fun `does not report when return value is explicitly ignored with underscore`() {
val code = """
Expand Down

0 comments on commit ef58f0e

Please sign in to comment.