From e66f1c2d70e49a54604f63b2948fee543a212f2c Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 12 Nov 2015 17:03:46 +0300 Subject: [PATCH] KT-9981 Code cleanup replace usages of !in incorrectly #KT-9981 Fixed --- .../CallableUsageReplacementStrategy.kt | 11 ++++++++-- .../deprecatedSymbolUsage/operatorCalls/in.kt | 20 ++++++++++++++++++ .../operatorCalls/in.kt.after | 20 ++++++++++++++++++ .../operatorCalls/plusAssign.kt | 12 +++++++++++ .../operatorCalls/plusAssign.kt.after | 12 +++++++++++ .../idea/quickfix/QuickFixTestGenerated.java | 21 +++++++++++++++++++ 6 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt.after create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/CallableUsageReplacementStrategy.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/CallableUsageReplacementStrategy.kt index 15ea9da2f9e..72b27ee0149 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/CallableUsageReplacementStrategy.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/CallableUsageReplacementStrategy.kt @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference import org.jetbrains.kotlin.idea.core.* +import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArgumentsIntention import org.jetbrains.kotlin.idea.intentions.setType import org.jetbrains.kotlin.idea.util.* @@ -65,8 +66,14 @@ class CallableUsageReplacementStrategy( if (!callTypeHandler.precheckReplacementPattern(replacement)) return null return { - // copy replacement expression because it is modified by performCallReplacement - performCallReplacement(usage, bindingContext, resolvedCall, callElement, callTypeHandler, replacement.copy()) + if (usage is KtOperationReferenceExpression && usage.getReferencedNameElementType() != KtTokens.IDENTIFIER) { + val nameExpression = OperatorToFunctionIntention.convert(usage.parent as KtExpression).second + createReplacer(nameExpression)!!.invoke() + } + else { + // copy replacement expression because it is modified by performCallReplacement + performCallReplacement(usage, bindingContext, resolvedCall, callElement, callTypeHandler, replacement.copy()) + } } } diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt b/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt new file mode 100644 index 00000000000..88f0f14db00 --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt @@ -0,0 +1,20 @@ +// "Replace usages of 'contains(String) on C: Boolean' in whole project" "true" + +class C + +@Deprecated("", ReplaceWith("checkContains(s)")) +operator fun C.contains(s: String) = true + +fun C.checkContains(s: String) = true + +fun f(c: C) { + if ("" !in c) { + + } +} + +fun g(c: C) { + if ("" in c) { + + } +} diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt.after new file mode 100644 index 00000000000..1033cff2ca2 --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt.after @@ -0,0 +1,20 @@ +// "Replace usages of 'contains(String) on C: Boolean' in whole project" "true" + +class C + +@Deprecated("", ReplaceWith("checkContains(s)")) +operator fun C.contains(s: String) = true + +fun C.checkContains(s: String) = true + +fun f(c: C) { + if (!c.checkContains("")) { + + } +} + +fun g(c: C) { + if (c.checkContains("")) { + + } +} diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt b/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt new file mode 100644 index 00000000000..971357b6909 --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt @@ -0,0 +1,12 @@ +// "Replace with 'add(c)'" "true" +class C + +@Deprecated("", ReplaceWith("add(c)")) +operator fun C.plus(c: C) = C() + +fun C.add(c: C) = C() + +fun f() { + var c1 = C() + c1 += C() +} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt.after new file mode 100644 index 00000000000..1249a741569 --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt.after @@ -0,0 +1,12 @@ +// "Replace with 'add(c)'" "true" +class C + +@Deprecated("", ReplaceWith("add(c)")) +operator fun C.plus(c: C) = C() + +fun C.add(c: C) = C() + +fun f() { + var c1 = C() + c1 = c1.add(C()) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index f20a0f05a94..ac07e950890 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -3730,6 +3730,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class OperatorCalls extends AbstractQuickFixTest { + public void testAllFilesPresentInOperatorCalls() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("in.kt") + public void testIn() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/in.kt"); + doTest(fileName); + } + + @TestMetadata("plusAssign.kt") + public void testPlusAssign() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/deprecatedSymbolUsage/operatorCalls/plusAssign.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/deprecatedSymbolUsage/optionalParameters") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)