Skip to content

Commit

Permalink
Change NestedScopeFunctions to early return on empty context
Browse files Browse the repository at this point in the history
  • Loading branch information
WildOrangutan committed May 2, 2022
1 parent bac48f7 commit d5368cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Expand Up @@ -16,6 +16,7 @@ import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall

/**
Expand Down Expand Up @@ -65,6 +66,7 @@ class NestedScopeFunctions(config: Config = Config.empty) : Rule(config) {
}

override fun visitNamedFunction(function: KtNamedFunction) {
if (bindingContext == BindingContext.EMPTY) return
function.accept(FunctionDepthVisitor())
}

Expand Down
Expand Up @@ -4,6 +4,7 @@ import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -93,10 +94,27 @@ class NestedScopeFunctionsSpec(private val env: KotlinCoreEnvironment) {
expectNoFindings()
}

@Test
fun `should not report when binding context is empty`() {
givenCode = """
fun f() {
1.run {
1.run { }
}
}
"""
whenLintRunsWithoutContext()
expectNoFindings()
}

private fun whenLintRuns() {
actual = subject.compileAndLintWithContext(env, givenCode)
}

private fun whenLintRunsWithoutContext() {
actual = subject.compileAndLint(givenCode)
}

private fun expectSourceLocation(location: Pair<Int, Int>) {
assertThat(actual).hasSourceLocation(location.first, location.second)
}
Expand Down

0 comments on commit d5368cb

Please sign in to comment.