|
| 1 | +/* |
| 2 | + * Minecraft Dev for IntelliJ |
| 3 | + * |
| 4 | + * https://minecraftdev.org |
| 5 | + * |
| 6 | + * Copyright (c) 2020 minecraft-dev |
| 7 | + * |
| 8 | + * MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +package com.demonwav.mcdev.platform.forge.inspections |
| 12 | + |
| 13 | +import com.demonwav.mcdev.platform.forge.util.ForgeConstants |
| 14 | +import com.intellij.psi.PsiErrorElement |
| 15 | +import com.intellij.psi.PsiExpression |
| 16 | +import com.intellij.psi.PsiLambdaExpression |
| 17 | +import com.intellij.psi.PsiMethodCallExpression |
| 18 | +import com.intellij.psi.PsiMethodReferenceExpression |
| 19 | +import com.siyeh.ig.BaseInspection |
| 20 | +import com.siyeh.ig.BaseInspectionVisitor |
| 21 | + |
| 22 | +class DistExecutorInspection : BaseInspection() { |
| 23 | + override fun getDisplayName() = "DistExecutor problems" |
| 24 | + |
| 25 | + override fun getStaticDescription() = "DistExecutor problems" |
| 26 | + |
| 27 | + override fun buildErrorString(vararg infos: Any?): String { |
| 28 | + return infos[0] as String |
| 29 | + } |
| 30 | + |
| 31 | + override fun buildVisitor(): BaseInspectionVisitor { |
| 32 | + return Visitor() |
| 33 | + } |
| 34 | + |
| 35 | + private class Visitor : BaseInspectionVisitor() { |
| 36 | + override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { |
| 37 | + val method = expression.resolveMethod() ?: return |
| 38 | + if (method.containingClass?.qualifiedName != ForgeConstants.DIST_EXECUTOR) return |
| 39 | + when (method.name) { |
| 40 | + "safeCallWhenOn", "safeRunWhenOn" -> { |
| 41 | + checkSafeArgument(method.name, expression.argumentList.expressions.getOrNull(1)) |
| 42 | + } |
| 43 | + "safeRunForDist" -> { |
| 44 | + for (arg in expression.argumentList.expressions) { |
| 45 | + checkSafeArgument(method.name, arg) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private fun checkSafeArgument(methodName: String, expression: PsiExpression?) { |
| 52 | + if (expression == null || expression is PsiErrorElement || expression.textLength == 0) return |
| 53 | + |
| 54 | + if (expression !is PsiLambdaExpression) { |
| 55 | + registerError(expression, "DistExecutor.$methodName must contain lambda argument") |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + val lambdaBody = expression.body |
| 60 | + if (lambdaBody != null && |
| 61 | + lambdaBody !is PsiMethodReferenceExpression && |
| 62 | + lambdaBody !is PsiErrorElement && |
| 63 | + lambdaBody.textLength != 0 |
| 64 | + ) { |
| 65 | + registerError(lambdaBody, "DistExecutor.$methodName must contain a method reference inside a lambda") |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments