From c077719c1be535c205c226c70841068093311f44 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 10 Nov 2020 10:50:48 +0800 Subject: [PATCH] Report NOT_CONSTRUCTABLE_TYPE on type argument --- .../kotlin/org/example/myplugin/MyPluginMain.kt | 2 +- .../kotlin/org/example/myplugin/MySimpleCommand.kt | 5 ++++- .../src/diagnostics/PluginDataValuesChecker.kt | 4 +++- .../src/diagnostics/diagnosticsUtil.kt | 14 +++++++++++++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MyPluginMain.kt b/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MyPluginMain.kt index 8856e0d1b..e59e512e3 100644 --- a/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MyPluginMain.kt +++ b/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MyPluginMain.kt @@ -30,8 +30,8 @@ object MyPluginMain : KotlinPlugin( } } + object DataTest : AutoSavePluginConfig("data") { - val p by value() val pp by value() } diff --git a/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MySimpleCommand.kt b/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MySimpleCommand.kt index 30699c49a..4e100351f 100644 --- a/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MySimpleCommand.kt +++ b/tools/intellij-plugin/run/projects/test-project/src/main/kotlin/org/example/myplugin/MySimpleCommand.kt @@ -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) { } -} +} \ No newline at end of file diff --git a/tools/intellij-plugin/src/diagnostics/PluginDataValuesChecker.kt b/tools/intellij-plugin/src/diagnostics/PluginDataValuesChecker.kt index 79b584b3f..274d7ea9c 100644 --- a/tools/intellij-plugin/src/diagnostics/PluginDataValuesChecker.kt +++ b/tools/intellij-plugin/src/diagnostics/PluginDataValuesChecker.kt @@ -43,13 +43,15 @@ class PluginDataValuesChecker : DeclarationChecker { val classDescriptor = type.constructor.declarationDescriptor?.castOrNull() 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()) ) diff --git a/tools/intellij-plugin/src/diagnostics/diagnosticsUtil.kt b/tools/intellij-plugin/src/diagnostics/diagnosticsUtil.kt index ac09c62f8..9b615da46 100644 --- a/tools/intellij-plugin/src/diagnostics/diagnosticsUtil.kt +++ b/tools/intellij-plugin/src/diagnostics/diagnosticsUtil.kt @@ -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? { return this.getResolvedCall(context.bindingContext) -} \ No newline at end of file +} + +fun KtTypeReference.isReferencing(fqName: FqName): Boolean { + return resolveReferencedType()?.getKotlinFqName() == fqName +} + +fun KtTypeReference.resolveReferencedType() = this.typeElement.castOrNull()?.referenceExpression?.mainReference?.resolve() \ No newline at end of file