UElementAsPsiInspection also checks casts

This commit is contained in:
Nicolay Mitropolsky
2017-12-23 10:54:50 +03:00
parent 74238b55e1
commit 4bbec3604f
3 changed files with 49 additions and 0 deletions
@@ -85,6 +85,24 @@ class KtUElementAsPsiInspectionTest : LightCodeInsightFixtureTestCase() {
}
fun testCast() {
myFixture.configureByText("UastUsage.kt", """
import org.jetbrains.uast.*;
import com.intellij.psi.PsiClass;
class UastUsage {
constructor(uElement: UElement){
(<warning descr="Usage of UElement as PsiElement is not recommended">uElement</warning> as? PsiClass).toString();
(uElement as? UClass).toString();
}
}
""".trimIndent())
myFixture.testHighlighting("UastUsage.kt")
}
fun testCallParentMethod() {
myFixture.configureByText("UastUsage.kt", """
import org.jetbrains.uast.UClass;
@@ -100,6 +100,13 @@ class UElementAsPsiInspection : DevKitUastInspectionBase() {
return false;
}
override fun visitBinaryExpressionWithType(node: UBinaryExpressionWithType): Boolean {
if (isUElementType(node.operand.getExpressionType()) && isPsiElementType(node.typeReference?.type)) {
node.operand.sourcePsiElement?.let { reportedElements.add(it) }
}
return false
}
override fun visitVariable(node: UVariable): Boolean {
if (isUElementType(node.uastInitializer?.getExpressionType()) && isPsiElementType(node.type)) {
node.uastInitializer.sourcePsiElement?.let { reportedElements.add(it) }
@@ -67,6 +67,30 @@ class UElementAsPsiInspectionTest : PluginModuleTestCase() {
}
fun testCast() {
//language=JAVA
myFixture.addClass("""
import org.jetbrains.uast.UClass;
import com.intellij.psi.PsiClass;
import org.jetbrains.uast.UElement;
class UastUsage {
public UastUsage(UElement uElement){
if(<warning descr="Usage of UElement as PsiElement is not recommended">uElement</warning> instanceof PsiClass){
PsiClass psiClass = (PsiClass)<warning descr="Usage of UElement as PsiElement is not recommended">uElement</warning>;
}
if(uElement instanceof UClass){
UClass uClass = (UClass)uElement;
}
}
}
""".trimIndent())
myFixture.testHighlighting("UastUsage.java")
}
fun testCallParentMethod() {
//language=JAVA
myFixture.addClass("""