UElementAsPsiInspection made to not report if method is redefined in UAST interfaces

because it is more like a collision in names (which was OK before we decided to unbound UElements from PsiElements) than a wrong usage.
This commit is contained in:
Nicolay Mitropolsky
2017-12-20 15:21:37 +03:00
parent 001696ef48
commit 6f68c2233c
2 changed files with 33 additions and 2 deletions
@@ -83,8 +83,12 @@ class UElementAsPsiInspection : DevKitUastInspectionBase() {
private fun checkReceiver(node: UCallExpression) {
if (!isUElementType(node.receiverType)) return
val psiMethod = node.resolve() ?: return
if (isPsiElementClass(psiMethod.containingClass) || psiMethod.findSuperMethods().any { isPsiElementClass(it.containingClass) }) {
node.sourcePsiElement?.let { reportedElements.add(it) }
val containingClass = psiMethod.containingClass ?: return
if (containingClass.qualifiedName in ALLOWED_REDEFINITION) return
if (!isPsiElementClass(containingClass) && psiMethod.findSuperMethods().none { isPsiElementClass(it.containingClass) }) return
if (psiMethod.findSuperMethods().any { it.containingClass?.qualifiedName in ALLOWED_REDEFINITION }) return
node.sourcePsiElement?.let {
reportedElements.add(it)
}
}
@@ -118,4 +122,13 @@ class UElementAsPsiInspection : DevKitUastInspectionBase() {
private fun psiClassType(fqn: String, searchScope: GlobalSearchScope): PsiClassType? =
PsiType.getTypeByName(fqn, searchScope.project, searchScope).takeIf { it.resolve() != null }
private companion object {
val ALLOWED_REDEFINITION = setOf(
UClass::class.java.name,
UMethod::class.java.name,
UVariable::class.java.name,
UClassInitializer::class.java.name
)
}
}
@@ -10,10 +10,14 @@ class UElementAsPsiInspectionTest : PluginModuleTestCase() {
myFixture.addClass("package org.jetbrains.uast; public interface UElement {}")
myFixture.addClass("""package com.intellij.psi; public interface PsiElement {
| PsiElement getParent();
| PsiElement getSelf();
|}""".trimMargin())
myFixture.addClass("package com.intellij.psi; public interface PsiClass extends PsiElement {}")
myFixture.addClass("""package org.jetbrains.uast; public interface UClass extends UElement, com.intellij.psi.PsiClass {
| void uClassMethod();
|
| @Override
| UClass getSelf();
|}""".trimMargin())
myFixture.enableInspections(UElementAsPsiInspection())
@@ -94,12 +98,26 @@ class UElementAsPsiInspectionTest : PluginModuleTestCase() {
UClassImpl impl = new UClassImpl();
<warning descr="Usage of UElement as PsiElement is not recommended">impl.getParent()</warning>;
impl.uClassMethod();
impl.getSelf();
}
public UastUsage(UClass impl){
<warning descr="Usage of UElement as PsiElement is not recommended">impl.getParent()</warning>;
impl.uClassMethod();
impl.getSelf();
}
}
class UClassImpl implements UClass {
@Override
public PsiElement getParent(){ return null; }
@Override
public UClass getSelf(){ return this; }
@Override
public void uClassMethod(){ }
}