Skip to content

Commit

Permalink
Enable Type Resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldickson committed Oct 13, 2024
1 parent c3bc574 commit 798840f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .idea/detekt.xml

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

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Expand Down Expand Up @@ -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()
}
Expand All @@ -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()
}
Expand All @@ -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 ->
Expand Down

0 comments on commit 798840f

Please sign in to comment.