Skip to content

Commit

Permalink
Detekt Unsued Returns
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldickson committed Oct 12, 2024
1 parent 3113733 commit 4edcb91
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 34 deletions.
9 changes: 6 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation("io.gitlab.arturbosch.detekt:detekt-test:1.22.0")
testImplementation("org.mockito:mockito-core:4.8.1")
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
testImplementation("org.assertj:assertj-core:3.21.0")
}

tasks.test {
Expand Down
19 changes: 0 additions & 19 deletions src/main/kotlin/io/agodadev/kraftdetekt/CustomDetektRule.kt

This file was deleted.

47 changes: 47 additions & 0 deletions src/main/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.agodadev.kraftdetekt

import io.gitlab.arturbosch.detekt.api.*
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext.isNothing
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext.isUnit

class IgnoredReturnValueRule(config: Config) : Rule(config) {
override val issue = Issue(
javaClass.simpleName,
Severity.Warning,
"This rule reports when a function call's return value is ignored.",
Debt.FIVE_MINS
)

override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)

val parent = expression.parent
if (parent is KtExpression && !parent.isUsedAsExpression(bindingContext)) {
val resolvedCall = expression.getResolvedCall(bindingContext) ?: return
val returnType = resolvedCall.resultingDescriptor.returnType ?: return

if (!returnType.isUnit() && !returnType.isNothing()) {
report(CodeSmell(
issue,
Entity.from(expression),
"The return value of this function call is ignored."
))
}
}
}
}

class IgnoredReturnValueRuleProvider : RuleSetProvider {
override val ruleSetId: String = "custom-rules"

override fun instance(config: Config): RuleSet {
return RuleSet(
ruleSetId,
listOf(IgnoredReturnValueRule(config))
)
}
}
69 changes: 61 additions & 8 deletions src/test/kotlin/io/agodadev/kraftdetekt/CustomDetektRuleTest.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@
package io.agodadev.kraftdetekt

import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.TestConfig
import org.junit.jupiter.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class CustomDetektRuleTest {
class IgnoredReturnValueRuleTest {

@Test
fun `reports ignored return values`() {
val code = """
fun returnsString(): String = "Hello"
fun returnsInt(): Int = 42
fun returnsNothing(): Nothing = throw Exception("Nothing")
fun returnsUnit(): Unit {}
fun test() {
returnsString()
returnsInt()
returnsNothing()
returnsUnit()
val x = returnsString()
if (returnsInt() > 0) {}
}
""".trimIndent()

val findings = IgnoredReturnValueRule(Config.empty).compileAndLint(code)

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.")
}

@Test
fun test1() {
val config = TestConfig()
val rule = CustomDetektRule(config)
Assertions.assertTrue(rule.issue.severity == io.gitlab.arturbosch.detekt.api.Severity.Warning)
fun `does not report when return value is used`() {
val code = """
fun returnsString(): String = "Hello"
fun returnsInt(): Int = 42
fun test() {
val s = returnsString()
println(returnsInt())
if (returnsString().isNotEmpty()) {}
}
""".trimIndent()

val findings = IgnoredReturnValueRule(Config.empty).compileAndLint(code)

assertThat(findings).isEmpty()
}

}
@Test
fun `does not report Unit and Nothing return types`() {
val code = """
fun returnsNothing(): Nothing = throw Exception("Nothing")
fun returnsUnit(): Unit {}
fun test() {
returnsNothing()
returnsUnit()
}
""".trimIndent()

val findings = IgnoredReturnValueRule(Config.empty).compileAndLint(code)

assertThat(findings).isEmpty()
}
}

0 comments on commit 4edcb91

Please sign in to comment.