From 6f68c2233c91080b5cc6601cc6cd819bdb4348c8 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Wed, 20 Dec 2017 13:41:59 +0300 Subject: [PATCH] `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. --- .../src/inspections/UElementAsPsiInspection.kt | 17 +++++++++++++++-- .../inspections/UElementAsPsiInspectionTest.kt | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/plugins/devkit/src/inspections/UElementAsPsiInspection.kt b/plugins/devkit/src/inspections/UElementAsPsiInspection.kt index 6c8537e73613..2f82a7e0ccdc 100644 --- a/plugins/devkit/src/inspections/UElementAsPsiInspection.kt +++ b/plugins/devkit/src/inspections/UElementAsPsiInspection.kt @@ -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 + ) + } + } \ No newline at end of file diff --git a/plugins/devkit/testSources/inspections/UElementAsPsiInspectionTest.kt b/plugins/devkit/testSources/inspections/UElementAsPsiInspectionTest.kt index 928d14177857..bed52020d728 100644 --- a/plugins/devkit/testSources/inspections/UElementAsPsiInspectionTest.kt +++ b/plugins/devkit/testSources/inspections/UElementAsPsiInspectionTest.kt @@ -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(); impl.getParent(); impl.uClassMethod(); + impl.getSelf(); + } + + public UastUsage(UClass impl){ + impl.getParent(); + impl.uClassMethod(); + impl.getSelf(); } } class UClassImpl implements UClass { + + @Override public PsiElement getParent(){ return null; } + + @Override + public UClass getSelf(){ return this; } + + @Override public void uClassMethod(){ } }