IDEA-273958 - fixed declaration scope for patterns in the scopes that differ from switch

GitOrigin-RevId: 8c8a835678a6c1de640e34277914b61e1542a1ab
This commit is contained in:
Ilyas Selimov
2021-07-30 09:08:34 +00:00
committed by intellij-monorepo-bot
parent 5c9c5e192c
commit cf1b781ca9
6 changed files with 46 additions and 5 deletions
@@ -113,7 +113,8 @@ public class PsiPatternVariableImpl extends CompositePsiElement implements PsiPa
}
return nextParent.getParent();
}
if (nextParent instanceof PsiPattern || nextParent instanceof PsiCaseLabelElementList) {
if (nextParent instanceof PsiPattern || nextParent instanceof PsiCaseLabelElementList ||
(parent instanceof PsiPattern && nextParent instanceof PsiInstanceOfExpression)) {
continue;
}
return parent;
@@ -0,0 +1,7 @@
class Main {
void test(Object o) {
if (o instanceof ((String <caret>s) && s.length() > 1)) {
}
s = "fsfsdfsd";
}
}
@@ -0,0 +1,11 @@
class Main {
void test(Object o) {
if (!(o instanceof ((String <caret>s) && s.length() > 1))) {
s = "fsfsdfsd"; // unresolved
}
else {
s = "fsfsdfsd";
System.out.println(s);
}
}
}
@@ -0,0 +1,6 @@
class Main {
void test(Object o) {
boolean b = o instanceof ((String <caret>s) && s.length() > 1);
s = "fsfsdfsd"; // unresolved
}
}
@@ -70,10 +70,20 @@ public class LightPatternsForSwitchHighlightingTest extends LightJavaCodeInsight
doTest();
}
public void testIdentifierHighlighterForPatternVariable() {
PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
PsiElement element = myFixture.getElementAtCaret();
assertSize(3, IdentifierHighlighterPass.getUsages(element, file, true));
public void testHighlighterForPatternVariableInSwitch() {
testIdentifierHighlighter(3);
}
public void testHighlighterForPatternVariableInIf() {
testIdentifierHighlighter(2);
}
public void testHighlighterForPatternVariableInIfElse() {
testIdentifierHighlighter(4);
}
public void testHighlighterForPatternVariableInLocalVariable() {
testIdentifierHighlighter(2);
}
public void testGuardWithInstanceOfPatternMatching() {
@@ -92,4 +102,10 @@ public class LightPatternsForSwitchHighlightingTest extends LightJavaCodeInsight
myFixture.configureByFile(getTestName(false) + ".java");
myFixture.checkHighlighting();
}
private void testIdentifierHighlighter(int expectedUsages) {
PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
PsiElement element = myFixture.getElementAtCaret();
assertSize(expectedUsages, IdentifierHighlighterPass.getUsages(element, file, true));
}
}