diff --git a/.idea/detekt.xml b/.idea/detekt.xml new file mode 100644 index 0000000..bc8dac5 --- /dev/null +++ b/.idea/detekt.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 4bfbbed..444b67b 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,17 @@ dependencies { } ``` +[Type Resolution](https://detekt.dev/docs/gettingstarted/type-resolution/) needs to be enabled for some of these rules to work + +As noted in docs about JVM/Gradle projects + +- detekt - Runs detekt `WITHOUT` type resolution +- detektMain - Runs detekt with type resolution on the `main` source set +- detektTest - Runs detekt with type resolution on the `test` source set + +So the best way to run these is to run two gradle jobs in CI, one for test and one for main, this will +also allow you to parallelize in CI, so not necessarily a bad thing imo. + ### Usage Configure Ktlint and Detekt in your project to use Agoda Kraft rules. Don't worry, they don't bite. diff --git a/src/main/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRule.kt b/src/main/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRule.kt index 7a6b42d..34269cb 100644 --- a/src/main/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRule.kt +++ b/src/main/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRule.kt @@ -1,12 +1,15 @@ package io.agodadev.kraftdetekt import io.gitlab.arturbosch.detekt.api.* +import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall import org.jetbrains.kotlin.types.typeUtil.isNothing import org.jetbrains.kotlin.types.typeUtil.isUnit +@RequiresTypeResolution class IgnoredReturnValueRule(config: Config) : Rule(config) { override val issue = Issue( javaClass.simpleName, @@ -23,6 +26,7 @@ class IgnoredReturnValueRule(config: Config) : Rule(config) { } val resolvedCall = expression.getResolvedCall(bindingContext) ?: return + val returnType = resolvedCall.resultingDescriptor.returnType ?: return if (!returnType.isUnit() && !returnType.isNothing()) { diff --git a/src/test/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRuleTest.kt b/src/test/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRuleTest.kt index 18ceedf..d760360 100644 --- a/src/test/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRuleTest.kt +++ b/src/test/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRuleTest.kt @@ -4,14 +4,13 @@ import io.gitlab.arturbosch.detekt.api.Config import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.github.detekt.test.utils.KotlinCoreEnvironmentWrapper import io.github.detekt.test.utils.createEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import org.assertj.core.api.Assertions.assertThat +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.junit.jupiter.api.Test -class IgnoredReturnValueRuleTest { - - private val wrapper = createEnvironment() - private val env = wrapper.env - +@KotlinCoreEnvironmentTest +class IgnoredReturnValueRuleTest(private val env: KotlinCoreEnvironment) { @Test fun `reports ignored return values`() { @@ -63,7 +62,7 @@ class IgnoredReturnValueRuleTest { } """.trimIndent() - val findings = IgnoredReturnValueRule(Config.empty).compileAndLintWithContext(wrapper.env, code) + val findings = IgnoredReturnValueRule(Config.empty).compileAndLintWithContext(env, code) assertThat(findings).isEmpty() } @@ -81,7 +80,7 @@ class IgnoredReturnValueRuleTest { } """.trimIndent() - val findings = IgnoredReturnValueRule(Config.empty).compileAndLintWithContext(wrapper.env, code) + val findings = IgnoredReturnValueRule(Config.empty).compileAndLintWithContext(env, code) assertThat(findings).isEmpty() } @@ -98,7 +97,7 @@ class IgnoredReturnValueRuleTest { } """.trimIndent() - val findings = IgnoredReturnValueRule(Config.empty).compileAndLintWithContext(wrapper.env, code) + val findings = IgnoredReturnValueRule(Config.empty).compileAndLintWithContext(env, code) println("Number of findings: ${findings.size}") findings.forEachIndexed { index, finding ->