Report NOT_CONSTRUCTABLE_TYPE on type argument

This commit is contained in:
Him188 2020-11-10 10:50:48 +08:00
parent 233ff9bee3
commit c077719c1b
4 changed files with 21 additions and 4 deletions

View File

@ -30,8 +30,8 @@ object MyPluginMain : KotlinPlugin(
}
}
object DataTest : AutoSavePluginConfig("data") {
val p by value<HasDefaultValue>()
val pp by value<NoDefaultValue>()
}

View File

@ -1,7 +1,10 @@
package org.example.myplugin
import kotlinx.serialization.Serializable
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.data.AutoSavePluginConfig
import net.mamoe.mirai.console.data.value
object MySimpleCommand000 : SimpleCommand(
MyPluginMain, "foo",
@ -11,4 +14,4 @@ object MySimpleCommand000 : SimpleCommand(
suspend fun CommandSender.handle(int: Int, str: String) {
}
}
}

View File

@ -43,13 +43,15 @@ class PluginDataValuesChecker : DeclarationChecker {
val classDescriptor = type.constructor.declarationDescriptor?.castOrNull<ClassDescriptor>()
val inspectionTarget: PsiElement by lazy {
callExpr.typeArguments.find { it.references.firstOrNull()?.canonicalText == type.fqName?.toString() } ?: callExpr
val fqName = type.fqName ?: return@lazy callExpr
callExpr.typeArguments.find { it.typeReference?.isReferencing(fqName) == true } ?: callExpr
}
if (classDescriptor == null
|| !classDescriptor.hasNoArgConstructor()
) return@forEach context.report(MiraiConsoleErrors.NOT_CONSTRUCTABLE_TYPE.on(
inspectionTarget,
callExpr,
type.fqName?.asString().toString())
)

View File

@ -9,10 +9,16 @@
package net.mamoe.mirai.console.intellij.diagnostics
import net.mamoe.mirai.console.compiler.common.castOrNull
import net.mamoe.mirai.console.intellij.resolve.getResolvedCall
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.psi.KtUserType
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
@ -26,4 +32,10 @@ fun KtElement?.getResolvedCall(
context: DeclarationCheckerContext,
): ResolvedCall<out CallableDescriptor>? {
return this.getResolvedCall(context.bindingContext)
}
}
fun KtTypeReference.isReferencing(fqName: FqName): Boolean {
return resolveReferencedType()?.getKotlinFqName() == fqName
}
fun KtTypeReference.resolveReferencedType() = this.typeElement.castOrNull<KtUserType>()?.referenceExpression?.mainReference?.resolve()