io.gitlab.arturbosch.detekt.rules.KtLambdaExpression.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of detekt-psi-utils Show documentation
Show all versions of detekt-psi-utils Show documentation
Static code analysis for Kotlin
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