IG: Support pattern matching for instanceof

GitOrigin-RevId: e35db4b8fffc27b8f9f5b5e380f24ff2a950f76e
This commit is contained in:
Andrey.Cherkasov
2021-08-05 12:37:53 +00:00
committed by intellij-monorepo-bot
parent 6c9454bcc5
commit cebacbf05f
3 changed files with 53 additions and 1 deletions
@@ -840,7 +840,16 @@ public class EquivalenceChecker {
}
final PsiTypeElement typeElement1 = instanceOfExpression1.getCheckType();
final PsiTypeElement typeElement2 = instanceOfExpression2.getCheckType();
return typeElementsAreEquivalent(typeElement1, typeElement2);
if (!typeElementsAreEquivalent(typeElement1, typeElement2).isExactMatch()) {
return EXACT_MISMATCH;
}
PsiPatternVariable patternVariable1 = JavaPsiPatternUtil.getPatternVariable(instanceOfExpression1.getPattern());
PsiPatternVariable patternVariable2 = JavaPsiPatternUtil.getPatternVariable(instanceOfExpression2.getPattern());
if (patternVariable1 == null || patternVariable2 == null) {
return Match.exact(patternVariable1 == patternVariable2);
}
markDeclarationsAsEquivalent(patternVariable1, patternVariable2);
return EXACT_MATCH;
}
protected Match typeElementsAreEquivalent(PsiTypeElement typeElement1, PsiTypeElement typeElement2) {
@@ -0,0 +1,39 @@
class Foo {
void test1(Object o) {
if (((o instanceof ((String s)) && ((s.length() > 42))))) {
System.out.println(s);
}
}
void test2(Object o) {
if (o instanceof String s && s.length() > 42) {
System.out.println(s);
}
}
void test3(Object o) {
if (o instanceof String s && s.length() > 42) {
System.out.println(s);
}
}
}
class Bar extends Foo {
void <warning descr="Method 'test1()' is identical to its super method">test1</warning>(Object o) {
if (o instanceof String str && str.length() > 42) {
System.out.println(str);
}
}
void test2(Object o) {
if (o instanceof CharSequence str && str.length() > 42) {
System.out.println(str);
}
}
void test3(Object o) {
if (o instanceof String str && str.length() > 0) {
System.out.println(str);
}
}
}
@@ -21,4 +21,8 @@ public class RedundantMethodOverrideInspection17Test extends LightJavaInspection
public void testSwitch() {
doTest();
}
public void testInstanceOf() {
doTest();
}
}