For each parameter unused: minor optimization / simplification
Related to KT-22068
This commit is contained in:
@@ -33,18 +33,16 @@ class ForEachParameterNotUsedInspection : AbstractKotlinInspection() {
|
|||||||
|
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
return callExpressionVisitor {
|
return callExpressionVisitor {
|
||||||
val calleeExpression = it.calleeExpression as? KtNameReferenceExpression ?: return@callExpressionVisitor
|
val calleeExpression = it.calleeExpression as? KtNameReferenceExpression
|
||||||
if (calleeExpression.getReferencedName() != FOREACH_NAME) return@callExpressionVisitor
|
if (calleeExpression?.getReferencedName() != FOREACH_NAME) return@callExpressionVisitor
|
||||||
|
val lambda = it.lambdaArguments.singleOrNull()?.getLambdaExpression()
|
||||||
|
if (lambda == null || lambda.functionLiteral.arrow != null) return@callExpressionVisitor
|
||||||
when (it.getCallableDescriptor()?.fqNameOrNull()) {
|
when (it.getCallableDescriptor()?.fqNameOrNull()) {
|
||||||
COLLECTIONS_FOREACH_FQNAME, SEQUENCES_FOREACH_FQNAME -> {
|
COLLECTIONS_FOREACH_FQNAME, SEQUENCES_FOREACH_FQNAME -> {
|
||||||
val lambda = it.lambdaArguments.singleOrNull()?.getLambdaExpression() ?: return@callExpressionVisitor
|
|
||||||
if (lambda.functionLiteral.arrow != null) return@callExpressionVisitor
|
|
||||||
val descriptor = lambda.analyze()[BindingContext.FUNCTION, lambda.functionLiteral] ?: return@callExpressionVisitor
|
val descriptor = lambda.analyze()[BindingContext.FUNCTION, lambda.functionLiteral] ?: return@callExpressionVisitor
|
||||||
val iterableParameter = descriptor.valueParameters.singleOrNull() ?: return@callExpressionVisitor
|
val iterableParameter = descriptor.valueParameters.singleOrNull() ?: return@callExpressionVisitor
|
||||||
|
|
||||||
if (iterableParameter !is WithDestructuringDeclaration &&
|
if (iterableParameter !is WithDestructuringDeclaration && !lambda.bodyExpression.usesDescriptor(iterableParameter)) {
|
||||||
lambda.bodyExpression?.usesDescriptor(iterableParameter) != true
|
|
||||||
) {
|
|
||||||
holder.registerProblem(
|
holder.registerProblem(
|
||||||
calleeExpression,
|
calleeExpression,
|
||||||
"Loop parameter '${iterableParameter.getThisLabelName()}' is unused",
|
"Loop parameter '${iterableParameter.getThisLabelName()}' is unused",
|
||||||
@@ -65,13 +63,14 @@ class ForEachParameterNotUsedInspection : AbstractKotlinInspection() {
|
|||||||
val literal = callExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()?.functionLiteral ?: return
|
val literal = callExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()?.functionLiteral ?: return
|
||||||
val psiFactory = KtPsiFactory(project)
|
val psiFactory = KtPsiFactory(project)
|
||||||
val newParameterList = psiFactory.createLambdaParameterList("_")
|
val newParameterList = psiFactory.createLambdaParameterList("_")
|
||||||
with (SpecifyExplicitLambdaSignatureIntention) {
|
with(SpecifyExplicitLambdaSignatureIntention) {
|
||||||
literal.setParameterListIfAny(psiFactory, newParameterList)
|
literal.setParameterListIfAny(psiFactory, newParameterList)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtBlockExpression.usesDescriptor(descriptor: VariableDescriptor): Boolean {
|
private fun KtBlockExpression?.usesDescriptor(descriptor: VariableDescriptor): Boolean {
|
||||||
|
if (this == null) return false
|
||||||
var used = false
|
var used = false
|
||||||
acceptChildren(object : KtVisitorVoid() {
|
acceptChildren(object : KtVisitorVoid() {
|
||||||
override fun visitKtElement(element: KtElement) {
|
override fun visitKtElement(element: KtElement) {
|
||||||
|
|||||||
Reference in New Issue
Block a user