-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
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)) | ||
) | ||
} | ||
} |
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() | ||
} | ||
} |