All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gitlab.arturbosch.detekt.rules.KtLambdaExpression.kt Maven / Gradle / Ivy

There is a newer version: 1.23.7
Show newest version
package io.gitlab.arturbosch.detekt.rules

import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall

fun KtLambdaExpression.firstParameter(bindingContext: BindingContext) =
    bindingContext[BindingContext.FUNCTION, functionLiteral]?.valueParameters?.singleOrNull()

fun KtLambdaExpression.implicitParameter(bindingContext: BindingContext): ValueParameterDescriptor? =
    if (valueParameters.isNotEmpty()) {
        null
    } else {
        firstParameter(bindingContext)
    }

fun KtLambdaExpression.hasImplicitParameterReference(
    implicitParameter: ValueParameterDescriptor,
    bindingContext: BindingContext
): Boolean {
    return anyDescendantOfType {
        it.isImplicitParameterReference(this, implicitParameter, bindingContext)
    }
}

private fun KtNameReferenceExpression.isImplicitParameterReference(
    lambda: KtLambdaExpression,
    implicitParameter: ValueParameterDescriptor,
    bindingContext: BindingContext
): Boolean {
    return text == "it" &&
        getStrictParentOfType() == lambda &&
        getResolvedCall(bindingContext)?.resultingDescriptor == implicitParameter
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy