Code cleanup

This commit is contained in:
Him188 2020-12-30 16:44:58 +08:00
parent 203636d309
commit 280a85ad8f
3 changed files with 3 additions and 23 deletions

View File

@ -42,9 +42,7 @@ class UsingStringPlusMessageInspection : AbstractKotlinInspection() {
if (className != "kotlin.String") return@visitor if (className != "kotlin.String") return@visitor
if (callee.name != "plus") return@visitor if (callee.name != "plus") return@visitor
val parent = expression.parent val inspectionTarget = when (val parent = expression.parent) {
val inspectionTarget = when (parent) {
is KtBinaryExpression -> { is KtBinaryExpression -> {
val right = parent.right?.referenceExpression()?.resolve() as? KtDeclaration ?: return@visitor val right = parent.right?.referenceExpression()?.resolve() as? KtDeclaration ?: return@visitor
val rightType = right.type() ?: return@visitor val rightType = right.type() ?: return@visitor
@ -65,8 +63,6 @@ class UsingStringPlusMessageInspection : AbstractKotlinInspection() {
else -> null else -> null
} ?: return@visitor } ?: return@visitor
println(expression::class.qualifiedName + " " + callee::class.qualifiedName + " " + callee.text)
holder.registerProblem( holder.registerProblem(
inspectionTarget, inspectionTarget,
"使用 String + Message 会导致 Message 被转换为 String 再相加", "使用 String + Message 会导致 Message 被转换为 String 再相加",

View File

@ -11,9 +11,7 @@ package net.mamoe.mirai.console.intellij.diagnostics.fix
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
@ -37,7 +35,6 @@ import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
import org.jetbrains.kotlin.psi.psiUtil.referenceExpression import org.jetbrains.kotlin.psi.psiUtil.referenceExpression
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.utils.checkWithAttachment
class ConvertToPlainTextFix( class ConvertToPlainTextFix(
/** /**
@ -52,19 +49,6 @@ class ConvertToPlainTextFix(
override fun getFamilyName(): String = "Mirai Console" override fun getFamilyName(): String = "Mirai Console"
override fun getText(): String = "将 String 转换为 PlainText" override fun getText(): String = "将 String 转换为 PlainText"
fun <TDeclaration : KtDeclaration> KtPsiFactory.createAnalyzableDeclaration(@NonNls text: String, context: PsiElement): TDeclaration {
val file = createAnalyzableFile("Dummy.kt", text, context)
val declarations = file.declarations
checkWithAttachment(declarations.size == 1, { "unexpected ${declarations.size} declarations" }) {
it.withAttachment("text.kt", text)
for (d in declarations.withIndex()) {
it.withAttachment("declaration${d.index}.kt", d.value.text)
}
}
@Suppress("UNCHECKED_CAST")
return declarations.first() as TDeclaration
}
override fun invokeImpl(project: Project, editor: Editor?, file: PsiFile) { override fun invokeImpl(project: Project, editor: Editor?, file: PsiFile) {
if (editor == null) return if (editor == null) return
if (file !is KtFile) return if (file !is KtFile) return

View File

@ -89,8 +89,8 @@ fun KtConstructorCalleeExpression.getTypeAsUserType(): KtUserType? {
} }
fun KotlinType.hasSuperType(fqName: String, includeSelf: Boolean = true): Boolean { fun KotlinType.hasSuperType(fqName: String, includeSelf: Boolean = true): Boolean {
if (this.fqName?.asString() == fqName) return true if (includeSelf && this.fqName?.asString() == fqName) return true
return this.supertypes().any { it.hasSuperType("net.mamoe.mirai.message.data.Message", false) } return this.supertypes().any { it.hasSuperType("net.mamoe.mirai.message.data.Message", true) }
} }
fun KtClassOrObject.hasSuperType(fqName: FqName): Boolean = allSuperNames.contains(fqName) fun KtClassOrObject.hasSuperType(fqName: FqName): Boolean = allSuperNames.contains(fqName)