From 2b61bca6dd1332f85b61ec8bcfde944034d462e6 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 27 May 2015 21:30:28 +0300 Subject: [PATCH] Code cleanup includes deprecated symbol usage fixes + code cleanup does not crash on nested usages to fix --- .../idea/inspections/KotlinCleanupInspection.kt | 16 ++++++++++++++-- .../idea/quickfix/DeprecatedSymbolUsageFix.kt | 2 +- idea/testData/inspections/cleanup/cleanup.kt | 3 +++ .../inspections/cleanup/cleanup.kt.after | 3 +++ .../inspections/cleanup/deprecatedSymbols.kt | 7 +++++++ .../inspections/KotlinCleanupInspectionTest.kt | 2 +- 6 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 idea/testData/inspections/cleanup/deprecatedSymbols.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt index d3e0495459c..ce13861a01c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt @@ -16,8 +16,11 @@ package org.jetbrains.kotlin.idea.inspections +import com.intellij.codeInsight.intention.IntentionAction import com.intellij.codeInspection.* +import com.intellij.openapi.editor.Editor import com.intellij.openapi.progress.ProcessCanceledException +import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.PsiJavaFile @@ -90,7 +93,8 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe Errors.UNNECESSARY_SAFE_CALL, Errors.USELESS_CAST, Errors.USELESS_ELVIS, - ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION + ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION, + Errors.DEPRECATED_SYMBOL_WITH_MESSAGE ) private fun Diagnostic.isObsoleteLabel(): Boolean { @@ -98,10 +102,18 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe return ReplaceObsoleteLabelSyntaxFix.looksLikeObsoleteLabel(annotationEntry) } + private class Wrapper(val intention: IntentionAction, file: JetFile) : IntentionWrapper(intention, file) { + override fun invoke(project: Project, editor: Editor?, file: PsiFile?) { + if (intention.isAvailable(project, editor, file)) { // we should check isAvailable here because some elements may get invalidated (or other conditions may change) + super.invoke(project, editor, file) + } + } + } + private fun Diagnostic.toProblemDescriptor(file: JetFile, manager: InspectionManager): ProblemDescriptor? { val quickFixes = JetPsiChecker.createQuickfixes(this) .filter { it is CleanupFix } - .map { IntentionWrapper(it, file) } + .map { Wrapper(it, file) } if (quickFixes.isEmpty()) return null diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt index 86d684eb60e..71398078399 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFix.kt @@ -31,7 +31,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall public class DeprecatedSymbolUsageFix( element: JetSimpleNameExpression/*TODO?*/, replaceWith: ReplaceWith -) : DeprecatedSymbolUsageFixBase(element, replaceWith), HighPriorityAction { +) : DeprecatedSymbolUsageFixBase(element, replaceWith), CleanupFix, HighPriorityAction { override fun getFamilyName() = "Replace deprecated symbol usage" diff --git a/idea/testData/inspections/cleanup/cleanup.kt b/idea/testData/inspections/cleanup/cleanup.kt index 12044cae24f..67dc77c8dfe 100644 --- a/idea/testData/inspections/cleanup/cleanup.kt +++ b/idea/testData/inspections/cleanup/cleanup.kt @@ -26,9 +26,12 @@ val x = fun foo(x: String) { } fun foo() { @loop for (i in 1..100) { + val v = oldFun2(i as Int) as Int /* comment */ continue@loop } + + oldFun1(oldFun2(10)) } fun unnecessarySafeCall(x: String) { diff --git a/idea/testData/inspections/cleanup/cleanup.kt.after b/idea/testData/inspections/cleanup/cleanup.kt.after index e280ee4d6b0..31cc69c08a2 100644 --- a/idea/testData/inspections/cleanup/cleanup.kt.after +++ b/idea/testData/inspections/cleanup/cleanup.kt.after @@ -28,9 +28,12 @@ val x = fun(x: String) { } fun foo() { loop@ for (i in 1..100) { + val v = oldFun2(i as Int) /* comment */ continue@loop } + + bar(bar(10 + 2) + 1) } fun unnecessarySafeCall(x: String) { diff --git a/idea/testData/inspections/cleanup/deprecatedSymbols.kt b/idea/testData/inspections/cleanup/deprecatedSymbols.kt new file mode 100644 index 00000000000..4b725f61944 --- /dev/null +++ b/idea/testData/inspections/cleanup/deprecatedSymbols.kt @@ -0,0 +1,7 @@ +@deprecated("", ReplaceWith("bar(p + 1)")) +public fun oldFun1(p: Int): Int = 0 + +@deprecated("", ReplaceWith("bar(p + 2)")) +public fun oldFun2(p: Int): Int = 0 + +public fun bar(p: Int): Int = p \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspectionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspectionTest.kt index 1cce2b66e24..69e7db890d9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspectionTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspectionTest.kt @@ -46,7 +46,7 @@ class KotlinCleanupInspectionTest(): JetLightCodeInsightFixtureTestCase() { } public fun testCleanup() { - doTest("cleanup.kt.after", "cleanup.kt", "JavaAnn.java") + doTest("cleanup.kt.after", "cleanup.kt", "JavaAnn.java", "deprecatedSymbols.kt") } public fun testDeprecatedFunctionClasses() {