diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/JavaStructuralSearchProfile.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/JavaStructuralSearchProfile.java index c61f27201cd7..011172a0b768 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/JavaStructuralSearchProfile.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/JavaStructuralSearchProfile.java @@ -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; } diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/handlers/DeclarationStatementHandler.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/handlers/DeclarationStatementHandler.java index 5ed977250a24..728fc0110c91 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/handlers/DeclarationStatementHandler.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/handlers/DeclarationStatementHandler.java @@ -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), diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ReferencePredicate.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ReferencePredicate.java index c039110472fd..f63a23f657fa 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ReferencePredicate.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ReferencePredicate.java @@ -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 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)); + }); } } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/SSBasedInspection.java b/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/SSBasedInspection.java index a41deac229b5..4d5916a4c01d 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/SSBasedInspection.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/SSBasedInspection.java @@ -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); }