Skip to content

Commit 1f32ebe

Browse files
committed
Fix inspection fixes taht may use invalid elements
Fixes minecraft-dev/mcdev-error-report#1464
1 parent 2745989 commit 1f32ebe

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/main/kotlin/platform/bukkit/inspection/BukkitListenerImplementedInspection.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.siyeh.ig.BaseInspection
3030
import com.siyeh.ig.BaseInspectionVisitor
3131
import com.siyeh.ig.InspectionGadgetsFix
3232
import org.jetbrains.annotations.Nls
33+
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
3334

3435
class BukkitListenerImplementedInspection : BaseInspection() {
3536

@@ -44,10 +45,10 @@ class BukkitListenerImplementedInspection : BaseInspection() {
4445
"All Bukkit @EventHandler methods must reside in a class that implements Listener."
4546

4647
override fun buildFix(vararg infos: Any): InspectionGadgetsFix {
48+
val classPointer = (infos[0] as PsiClass).createSmartPointer()
4749
return object : InspectionGadgetsFix() {
4850
override fun doFix(project: Project, descriptor: ProblemDescriptor) {
49-
val psiClass = infos[0] as PsiClass
50-
psiClass.addImplements(BukkitConstants.LISTENER_CLASS)
51+
classPointer.element?.addImplements(BukkitConstants.LISTENER_CLASS)
5152
}
5253

5354
@Nls
@@ -61,11 +62,7 @@ class BukkitListenerImplementedInspection : BaseInspection() {
6162
override fun buildVisitor(): BaseInspectionVisitor {
6263
return object : BaseInspectionVisitor() {
6364
override fun visitClass(aClass: PsiClass) {
64-
if (
65-
aClass.methods.none {
66-
it.modifierList.findAnnotation(BukkitConstants.HANDLER_ANNOTATION) != null
67-
}
68-
) {
65+
if (aClass.methods.none { it.hasAnnotation(BukkitConstants.HANDLER_ANNOTATION) }) {
6966
return
7067
}
7168

src/main/kotlin/platform/bungeecord/inspection/BungeeCordListenerImplementedInspection.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.siyeh.ig.BaseInspection
3030
import com.siyeh.ig.BaseInspectionVisitor
3131
import com.siyeh.ig.InspectionGadgetsFix
3232
import org.jetbrains.annotations.Nls
33+
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
3334

3435
class BungeeCordListenerImplementedInspection : BaseInspection() {
3536

@@ -43,10 +44,10 @@ class BungeeCordListenerImplementedInspection : BaseInspection() {
4344
"All BungeeCord @EventHandler methods must reside in a class that implements Listener."
4445

4546
override fun buildFix(vararg infos: Any): InspectionGadgetsFix {
47+
val classPointer = (infos[0] as PsiClass).createSmartPointer()
4648
return object : InspectionGadgetsFix() {
4749
override fun doFix(project: Project, descriptor: ProblemDescriptor) {
48-
val psiClass = infos[0] as PsiClass
49-
psiClass.addImplements(BungeeCordConstants.LISTENER_CLASS)
50+
classPointer.element?.addImplements(BungeeCordConstants.LISTENER_CLASS)
5051
}
5152

5253
@Nls

src/main/kotlin/platform/mcp/inspections/StackEmptyInspection.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import com.siyeh.ig.BaseInspection
4040
import com.siyeh.ig.BaseInspectionVisitor
4141
import com.siyeh.ig.InspectionGadgetsFix
4242
import org.jetbrains.annotations.Nls
43+
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
4344

4445
class StackEmptyInspection : BaseInspection() {
4546
companion object {
@@ -63,14 +64,17 @@ class StackEmptyInspection : BaseInspection() {
6364
" the stack should still be considered empty. Instead, isEmpty() should be called."
6465

6566
override fun buildFix(vararg infos: Any): InspectionGadgetsFix {
67+
val compareExpressionPointer = (infos[0] as PsiExpression).createSmartPointer()
68+
val binaryExpressionPointer = (infos[2] as PsiBinaryExpression).createSmartPointer()
6669
return object : InspectionGadgetsFix() {
6770
override fun getFamilyName() = "Replace with .isEmpty()"
6871

6972
override fun doFix(project: Project, descriptor: ProblemDescriptor) {
70-
val compareExpression = infos[0] as PsiExpression
71-
val binaryExpression = infos[2] as PsiBinaryExpression
7273
val elementFactory = JavaPsiFacade.getElementFactory(project)
7374

75+
val compareExpression = compareExpressionPointer.element ?: return
76+
val binaryExpression = binaryExpressionPointer.element ?: return
77+
7478
val mappedIsEmpty = compareExpression.findModule()?.getMappedMethod(IS_EMPTY_SRG)?.name ?: "isEmpty"
7579

7680
var expressionText = "${compareExpression.text}.$mappedIsEmpty()"

0 commit comments

Comments
 (0)