Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnnecessaryLet: fix false positive with with invoke operator calls #5240

Merged
merged 1 commit into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,7 +23,6 @@ import org.jetbrains.kotlin.psi.KtSafeQualifiedExpression
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall

/**
* `let` expressions are used extensively in our code for null-checking and chaining functions,
Expand Down Expand Up @@ -124,7 +123,7 @@ private fun KtBlockExpression.hasOnlyOneStatement() = this.children.size == 1

private fun PsiElement.countVarRefs(parameter: CallableDescriptor, context: BindingContext): Int =
collectDescendantsOfType<KtSimpleNameExpression> {
it.getResolvedCall(context)?.resultingDescriptor == parameter
context[BindingContext.REFERENCE_TARGET, it] == parameter
}.count()

private fun KtLambdaExpression.countReferences(context: BindingContext): Int {
Expand Down
Expand Up @@ -413,6 +413,21 @@ class UnnecessaryLetSpec(val env: KotlinCoreEnvironment) {
val findings = subject.compileAndLintWithContext(env, content)
assertThat(findings).isEmpty()
}

@Test
fun `does not report lets with invoke operator calls`() {
val findings = subject.compileAndLintWithContext(
env,
"""
fun f(callback: ((Int) -> Int)?) {
callback?.let { that ->
that(42)
}
}
"""
)
assertThat(findings).isEmpty()
}
}

private const val MESSAGE_OMIT_LET = "let expression can be omitted"
Expand Down