[java-highlighting] IDEA-341846 Code with instanceof for a local class inside a generic function is marked as red

- skip methods to capture parameters

GitOrigin-RevId: df0c152d33bb9f880dad2428b7635d06209b3e0c
This commit is contained in:
Mikhail Pyltsin
2024-02-05 18:37:24 +01:00
committed by intellij-monorepo-bot
parent d1bc45d383
commit b4dd504e01
3 changed files with 35 additions and 1 deletions

View File

@@ -977,7 +977,17 @@ public final class PsiUtil extends PsiUtilCore {
if (currentOwner.hasModifierProperty(PsiModifier.STATIC)) break;
if (currentOwner instanceof PsiClass && isLocalClass((PsiClass)currentOwner)) {
currentOwner = PsiTreeUtil.getParentOfType(currentOwner, PsiTypeParameterListOwner.class);
//otherwise, parameters will be taken from methods
boolean ownerIsStatic = false;
do {
currentOwner = PsiTreeUtil.getParentOfType(currentOwner, PsiTypeParameterListOwner.class);
if (currentOwner != null && currentOwner.hasModifierProperty(PsiModifier.STATIC)) {
ownerIsStatic = true;
break;
}
}
while (currentOwner instanceof PsiMethod);
if (ownerIsStatic) break;
continue;
}
currentOwner = currentOwner.getContainingClass();

View File

@@ -0,0 +1,23 @@
class C<A> {
static <T> Object foo(Object x) {
class Local {
}
return (x instanceof Local) ? x : new Local();
}
static <T> Object foo2(Object x) {
class Local<T> {
}
return (x instanceof Local) ? x : new Local();
}
Object foo3(Object x) {
class Local {
}
return (x instanceof <error descr="Illegal generic type for instanceof">Local</error>) ? x : new Local();
}
public static void main(String[] args) {
System.out.println(C.<Integer>foo(""));
}
}

View File

@@ -1206,4 +1206,5 @@ public class GenericsHighlighting8Test extends LightDaemonAnalyzerTestCase {
public void testLowerBoundAssignabilityCheck() { doTest(); }
public void testIgnoreErasureForProperTypeBound() { doTest(); }
public void testInferenceErrorAttribution() {doTest();}
public void testLocalClassParameters() {doTest();}
}