From 376b4ffb49a3f66248f323fba898fc01ec570657 Mon Sep 17 00:00:00 2001 From: "Konstantin.Nisht" Date: Mon, 11 May 2020 19:19:27 +0300 Subject: [PATCH] [groovy] Avoid resolving untyped parameters in the light version of extended inference GitOrigin-RevId: 325d70ba8ad2c8dc026888395c9becaac97627f9 --- .../style/inference/search/searchUtils.kt | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/inference/search/searchUtils.kt b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/inference/search/searchUtils.kt index 8425a2ffe43a..828402a3a912 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/inference/search/searchUtils.kt +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/inference/search/searchUtils.kt @@ -10,7 +10,6 @@ import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.psi.util.parentOfType import com.intellij.psi.util.parentOfTypes import com.intellij.util.Processor -import com.intellij.util.containers.map2Array import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor import org.jetbrains.plugins.groovy.lang.psi.api.GrFunctionalExpression @@ -19,7 +18,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpres import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod @@ -45,7 +43,8 @@ private fun SearchRequestCollector.adoptSearchRequest(request: PsiSearchRequest, private class ScopeFilteringRequestProcessor(private val anchorElement: GrMethod, private val delegateProcessor: RequestResultProcessor) : RequestResultProcessor() { - private class CollisionFinder(vararg val bannedIdentifiers: String) : GroovyRecursiveElementVisitor() { + private class CollisionFinder(val bannedIdentifiers: List, + val bannedElements: List) : GroovyRecursiveElementVisitor() { var foundCollision = false override fun visitElement(element: GroovyPsiElement) { @@ -53,7 +52,8 @@ private class ScopeFilteringRequestProcessor(private val anchorElement: GrMethod } override fun visitReferenceExpression(referenceExpression: GrReferenceExpression) { - if (referenceExpression.referenceName in bannedIdentifiers) { + if (referenceExpression.referenceName in bannedIdentifiers || + referenceExpression.staticReference.resolve() in bannedElements) { foundCollision = true } else { @@ -71,19 +71,29 @@ private class ScopeFilteringRequestProcessor(private val anchorElement: GrMethod if (element.findElementAt(offsetInElement)?.parentOfType() != null) { return true } - val enclosingClosure: GrFunctionalExpression? = element.parentOfType() - val call: GrMethodCall? = enclosingClosure?.parentOfType() - val arguments: List? = call?.closureArguments?.asList()?.plus(call.expressionArguments.asList()) - if (arguments?.contains(enclosingClosure) == true && call.callReference?.methodName == anchorElement.name) { - return true - } - if (checkSelfReferencesInArguments(element)) { + val enclosingMethod: GrMethod? = element.parentOfType() + val bannedElements: List = enclosingMethod?.parameters?.filter { it.typeElement == null } ?: emptyList() + val collisionFinder = CollisionFinder(listOf(anchorElement.name), bannedElements) + if (hasSelfReferencesInCaller(element, collisionFinder) || hasSelfReferencesInArguments(element, collisionFinder)) { return true } return delegateProcessor.processTextOccurrence(element, offsetInElement, consumer) } - fun checkSelfReferencesInArguments(element: PsiElement): Boolean { + fun hasSelfReferencesInCaller(element: PsiElement, collisionFinder: CollisionFinder): Boolean { + val enclosingClosure: GrFunctionalExpression = element.parentOfType() ?: return false + val call: GrMethodCall = enclosingClosure.parentOfType() ?: return false + val arguments: List = call.closureArguments?.asList()?.plus(call.expressionArguments.asList()) ?: emptyList() + if (enclosingClosure !in arguments) { + return false + } + if (collisionFinder.apply(call::accept).foundCollision) { + return true + } + return hasSelfReferencesInCaller(call, collisionFinder) + } + + fun hasSelfReferencesInArguments(element: PsiElement, collisionFinder: CollisionFinder): Boolean { val expressionWithArguments: GrExpression? = element.parentOfTypes(GrMethodCall::class, GrAssignmentExpression::class) ?: return false val isCorrectlyPointing = when (expressionWithArguments) { is GrMethodCall -> expressionWithArguments.invokedExpression === element @@ -91,9 +101,6 @@ private class ScopeFilteringRequestProcessor(private val anchorElement: GrMethod else -> return false } if (isCorrectlyPointing) { - val enclosingMethod: GrMethod? = expressionWithArguments.parentOfType()?.takeIf { it.name == anchorElement.name } - val bannedIdentifiers: Array = enclosingMethod?.parameters?.map2Array { it.name } ?: emptyArray() - val collisionFinder = CollisionFinder(anchorElement.name, *bannedIdentifiers) val arguments = when (expressionWithArguments) { is GrMethodCall -> expressionWithArguments.expressionArguments is GrAssignmentExpression -> arrayOf(expressionWithArguments.rValue)