Additional minor fixes for KT-33594: avoid using hard-coded annotation name, simplify hasAnnotationToSuppressDeprecation()

This commit is contained in:
Yan Zhulanow
2020-10-06 18:30:34 +09:00
parent cf5a6274e2
commit 2a841550d4
@@ -20,8 +20,11 @@ import com.intellij.codeInsight.intention.HighPriorityAction
import com.intellij.codeInsight.intention.IntentionAction import com.intellij.codeInsight.intention.IntentionAction
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 org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.codeInliner.UsageReplacementStrategy import org.jetbrains.kotlin.idea.codeInliner.UsageReplacementStrategy
@@ -29,10 +32,7 @@ import org.jetbrains.kotlin.idea.core.moveCaret
import org.jetbrains.kotlin.idea.core.targetDescriptors import org.jetbrains.kotlin.idea.core.targetDescriptors
import org.jetbrains.kotlin.idea.quickfix.CleanupFix import org.jetbrains.kotlin.idea.quickfix.CleanupFix
import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.resolve.calls.callUtil.getCalleeExpressionIfAny import org.jetbrains.kotlin.resolve.calls.callUtil.getCalleeExpressionIfAny
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
@@ -66,16 +66,34 @@ class DeprecatedSymbolUsageFix(
} }
private fun KtFile.hasAnnotationToSuppressDeprecation(): Boolean { private fun KtFile.hasAnnotationToSuppressDeprecation(): Boolean {
val suppressAnnotationEntry = annotationEntries.firstOrNull { val suppressAnnotation = annotationEntries.firstOrNull { it.isSuppress() } ?: return false
it.shortName?.asString() == "Suppress"
&& it.resolveToCall()?.resultingDescriptor?.containingDeclaration?.fqNameSafe == KotlinBuiltIns.FQ_NAMES.suppress for (valueArgument in suppressAnnotation.valueArguments) {
} ?: return false val template = valueArgument.getArgumentExpression() as? KtStringTemplateExpression ?: continue
return suppressAnnotationEntry.valueArguments.any { val text = template.entries.singleOrNull()?.text ?: continue
if (text == Errors.DEPRECATION.name || text == Errors.DEPRECATION_ERROR.name) {
return true
}
}
return suppressAnnotation.valueArguments.any {
val text = (it.getArgumentExpression() as? KtStringTemplateExpression)?.entries?.singleOrNull()?.text ?: return@any false val text = (it.getArgumentExpression() as? KtStringTemplateExpression)?.entries?.singleOrNull()?.text ?: return@any false
text.equals("DEPRECATION", ignoreCase = true) text.equals("DEPRECATION", ignoreCase = true)
} }
} }
private fun KtAnnotationEntry.isSuppress(): Boolean {
val suppressName = StandardNames.FqNames.suppress
if (shortName != suppressName.shortName()) {
return false
}
val annotationDescriptor = resolveToCall()?.resultingDescriptor?.containingDeclaration as? ClassDescriptor ?: return false
return annotationDescriptor.kind == ClassKind.ANNOTATION_CLASS && annotationDescriptor.fqNameSafe == suppressName
}
private fun isImportToBeRemoved(import: KtImportDirective): Boolean { private fun isImportToBeRemoved(import: KtImportDirective): Boolean {
if (import.isAllUnder) return false if (import.isAllUnder) return false