IDEA-235734 false positive warning: inspection should check whether an element is PsiClass descendant

GitOrigin-RevId: 509445bd4a02c13d007b9c0cf37f559b2ec461bf
This commit is contained in:
Ilyas Selimov
2020-05-14 13:23:23 +00:00
committed by intellij-monorepo-bot
parent c5e88846c1
commit 82c7b69cd3
7 changed files with 23 additions and 8 deletions
@@ -112,8 +112,8 @@ public class TestOnlyInspection extends AbstractBaseJavaLocalInspectionTool {
if (vft == null && !isAnnotatedAsTestOnly(member)) {
return true;
}
if (isInsideTestOnlyMethod(place) || isInsideTestOnlyField(place) || isInsideTestClass(place) || isUnderTestSources(place)
|| isGenericType(place, member)) {
if (isInsideTestOnlyMethod(place) || isInsideTestOnlyField(place) || isInsideTestOnlyClass(place) || isInsideTestClass(place)
|| isUnderTestSources(place)) {
return true;
}
@@ -174,6 +174,10 @@ public class TestOnlyInspection extends AbstractBaseJavaLocalInspectionTool {
return isAnnotatedAsTestOnly(getTopLevelParentOfType(e, PsiField.class));
}
private static boolean isInsideTestOnlyClass(@NotNull PsiElement e) {
return isAnnotatedAsTestOnly(getTopLevelParentOfType(e, PsiClass.class));
}
private static boolean isAnnotatedAsTestOnly(@Nullable PsiMember m) {
if (m == null) return false;
return isDirectlyTestOnly(m) || isAnnotatedAsTestOnly(m.getContainingClass());
@@ -188,11 +192,6 @@ public class TestOnlyInspection extends AbstractBaseJavaLocalInspectionTool {
return c != null && TestFrameworks.getInstance().isTestClass(c);
}
private static boolean isGenericType(@NotNull PsiElement place, @NotNull PsiMember member) {
PsiClass parent = PsiTreeUtil.getParentOfType(place, PsiClass.class);
return member.equals(parent);
}
private static <T extends PsiElement> T getTopLevelParentOfType(PsiElement e, Class<T> c) {
T parent = PsiTreeUtil.getParentOfType(e, c);
if (parent == null) return null;
@@ -0,0 +1,8 @@
import org.jetbrains.annotations.TestOnly;
import java.util.List;
@TestOnly
public class Bar {
static abstract class Foo implements List<Bar> {
}
}
@@ -0,0 +1,8 @@
import org.jetbrains.annotations.TestOnly;
@TestOnly
public class Bar {
static {
java.lang.Class<Bar> clazz = Bar.class;
}
}
@@ -80,7 +80,7 @@ public class TestOnlyInspectionTest extends JavaInspectionTestCase {
doTest();
}
public void testGenericType() {
public void testInsideTestOnlyClass() {
doTest();
}