Java: avoid false positive in broken code with cyclic inheritance

for "Subclass field hides superclass field" inspection

GitOrigin-RevId: 2da814fb51c92618cf528260c0ee585b894ca65a
This commit is contained in:
Bas Leijdekkers
2025-06-17 10:25:58 +00:00
committed by intellij-monorepo-bot
parent 2a24d241f8
commit f48cea56e1
2 changed files with 11 additions and 1 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2019 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2025 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -97,6 +97,7 @@ public final class FieldHidesSuperclassFieldInspection extends BaseInspection {
}
PsiClass ancestorClass = aClass.getSuperClass();
final Set<PsiClass> visitedClasses = new HashSet<>();
visitedClasses.add(aClass);
while (ancestorClass != null) {
if (!visitedClasses.add(ancestorClass)) {
return;
@@ -10,4 +10,13 @@ class Sub extends FieldHidesSuperclassField {
class Sub2 extends FieldHidesSuperclassField {
String a;
String <warning descr="Field 's' hides field in superclass">s</warning>;
}
<error descr="Cyclic inheritance involving 'A'">class A extends B</error> {
String s = "";
void f() { }
}
<error descr="Cyclic inheritance involving 'B'">class B extends A</error> {
void f() { }
}