SSR: don't traverse psi elements in inspection (IDEA-273812)

GitOrigin-RevId: 1c2cbdc64e7486e65623a0231c894389a9e0eb1b
This commit is contained in:
Bas Leijdekkers
2021-08-01 09:58:25 +00:00
committed by intellij-monorepo-bot
parent 2e0a90a168
commit 497139d8a2
4 changed files with 16 additions and 7 deletions
@@ -189,8 +189,10 @@ public final class JavaStructuralSearchProfile extends StructuralSearchProfile {
public @NotNull PsiElement extendMatchedByDownUp(@NotNull PsiElement targetNode) {
if (targetNode instanceof PsiIdentifier) {
targetNode = targetNode.getParent();
final PsiElement parent = targetNode.getParent();
if (parent instanceof PsiTypeElement || parent instanceof PsiStatement) targetNode = parent;
}
final PsiElement parent = targetNode.getParent();
if (parent instanceof PsiTypeElement || parent instanceof PsiStatement || parent instanceof PsiLocalVariable) {
targetNode = parent;
}
return targetNode;
}
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch.impl.matcher.handlers;
import com.intellij.dupLocator.iterators.ArrayBackedNodeIterator;
@@ -36,7 +36,7 @@ public class DeclarationStatementHandler extends MatchingHandler {
final PsiElement[] declared = dcl.getDeclaredElements();
// declaration statement could wrap class or dcl
if (declared.length > 0 && (!context.shouldRecursivelyMatch() || !(matchedNode.getParent() instanceof PsiDeclarationStatement)) /* skip twice matching for child*/) {
if (declared.length > 0 && !(matchedNode.getParent() instanceof PsiDeclarationStatement) /* skip twice matching for child*/) {
if (!(matchedNode instanceof PsiField)) {
return context.getMatcher().matchSequentially(
new ArrayBackedNodeIterator(declared),
@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch.impl.matcher.predicates;
import com.intellij.openapi.fileTypes.LanguageFileType;
@@ -7,6 +7,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceService;
import com.intellij.structuralsearch.Matcher;
import com.intellij.structuralsearch.StructuralSearchProfile;
import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.impl.matcher.MatchContext;
import org.jetbrains.annotations.NotNull;
@@ -29,6 +30,9 @@ public final class ReferencePredicate extends MatchPredicate {
public boolean match(@NotNull PsiElement matchedNode, int start, int end, @NotNull MatchContext context) {
matchedNode = StructuralSearchUtil.getParentIfIdentifier(matchedNode);
final List<PsiReference> references = PsiReferenceService.getService().getReferences(matchedNode, PsiReferenceService.Hints.NO_HINTS);
return references.stream().map(PsiReference::resolve).filter(Objects::nonNull).anyMatch(t -> matcher.matchNode(t));
return references.stream().map(PsiReference::resolve).filter(Objects::nonNull).anyMatch(t -> {
StructuralSearchProfile profile = StructuralSearchUtil.getProfileByPsiElement(t);
return profile != null && matcher.matchNode(profile.extendMatchedByDownUp(t));
});
}
}
@@ -357,7 +357,10 @@ public class SSBasedInspection extends LocalInspectionTool implements DynamicGro
else {
final Matcher newMatcher = buildCompiledConfiguration(configuration, project);
if (newMatcher != null) {
newMatcher.getMatchContext().setSink(new InspectionResultSink());
MatchContext context = newMatcher.getMatchContext();
context.setSink(new InspectionResultSink());
// ssr should never match recursively because this is handled by the inspection visitor
context.setShouldRecursivelyMatch(false);
}
result.put(configuration, newMatcher);
}