From 3742993dea08484df803da0f398c1a71a9238660 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sun, 21 Jul 2019 21:37:18 +0900 Subject: [PATCH] Change to star projection: do not suggest when type paramer is generic type #KT-30794 Fixed --- .../quickfix/ChangeToStarProjectionFix.kt | 19 +++++++++- .../cast/genericTypeParameter.kt | 4 +++ .../cast/genericTypeParameter2.kt | 4 +++ .../cast/genericTypeParameter3.kt | 4 +++ .../cast/genericTypeParameter3.kt.after | 4 +++ .../cast/genericTypeParameter4.kt | 5 +++ .../cast/genericTypeParameter4.kt.after | 5 +++ .../cast/genericTypeParameter5.kt | 4 +++ .../cast/genericTypeParameter6.kt | 4 +++ .../cast/genericTypeParameter6.kt.after | 4 +++ .../cast/genericTypeParameter7.kt | 4 +++ .../cast/genericTypeParameter7.kt.after | 4 +++ .../idea/quickfix/QuickFixTestGenerated.java | 35 +++++++++++++++++++ 13 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter.kt create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter2.kt create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt.after create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt.after create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter5.kt create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt.after create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt create mode 100644 idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToStarProjectionFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToStarProjectionFix.kt index fd64d13b8e7..32e209eb399 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToStarProjectionFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToStarProjectionFix.kt @@ -20,13 +20,16 @@ import com.intellij.codeInsight.intention.IntentionAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypes import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType +import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.typeUtil.isTypeParameter class ChangeToStarProjectionFix(element: KtTypeElement) : KotlinQuickFixAction(element) { @@ -48,7 +51,17 @@ class ChangeToStarProjectionFix(element: KtTypeElement) : KotlinQuickFixAction { val callExpr = parent.getStrictParentOfType() @@ -75,5 +88,9 @@ class ChangeToStarProjectionFix(element: KtTypeElement) : KotlinQuickFixAction { + return (this?.typeElement as? KtUserType)?.typeArguments.orEmpty() + } } } diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter.kt b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter.kt new file mode 100644 index 00000000000..e5accba8d27 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter.kt @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "false" +// ACTION: Convert to block body +// ACTION: Introduce local variable +fun test(list: List<*>): List = list as List \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter2.kt b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter2.kt new file mode 100644 index 00000000000..cb2c5cdc0de --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter2.kt @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "false" +fun test(list: List<*>): List { + return list as List +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt new file mode 100644 index 00000000000..a4730578ece --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "true" +fun test(list: List<*>): List<*> { + return list as List +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt.after b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt.after new file mode 100644 index 00000000000..c253f89fd6e --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt.after @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "true" +fun test(list: List<*>): List<*> { + return list as List<*> +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt new file mode 100644 index 00000000000..ad5fd5870b3 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt @@ -0,0 +1,5 @@ +// "Change type arguments to <*>" "true" +fun test(list: List<*>): List { + list as List + return list as List +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt.after b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt.after new file mode 100644 index 00000000000..49756cbe68b --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt.after @@ -0,0 +1,5 @@ +// "Change type arguments to <*>" "true" +fun test(list: List<*>): List { + list as List<*> + return list as List +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter5.kt b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter5.kt new file mode 100644 index 00000000000..ee8296005cd --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter5.kt @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "false" +fun test(list: List<*>) { + val a: List = list as List +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt new file mode 100644 index 00000000000..023bc5a0e43 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "true" +fun test6(list: List<*>) { + val a: List<*> = list as List +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt.after b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt.after new file mode 100644 index 00000000000..6fba1ad0b89 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt.after @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "true" +fun test6(list: List<*>) { + val a: List<*> = list as List<*> +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt new file mode 100644 index 00000000000..eddfa597cd4 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "true" +fun test7(list: List<*>) { + val a = list as List +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt.after b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt.after new file mode 100644 index 00000000000..93d10b77ec3 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt.after @@ -0,0 +1,4 @@ +// "Change type arguments to <*>" "true" +fun test7(list: List<*>) { + val a = list as List<*> +} \ 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 c30789d6de9..af37ae9587a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -1186,6 +1186,41 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/addStarProjections/cast/extensionReceiver5.kt"); } + @TestMetadata("genericTypeParameter.kt") + public void testGenericTypeParameter() throws Exception { + runTest("idea/testData/quickfix/addStarProjections/cast/genericTypeParameter.kt"); + } + + @TestMetadata("genericTypeParameter2.kt") + public void testGenericTypeParameter2() throws Exception { + runTest("idea/testData/quickfix/addStarProjections/cast/genericTypeParameter2.kt"); + } + + @TestMetadata("genericTypeParameter3.kt") + public void testGenericTypeParameter3() throws Exception { + runTest("idea/testData/quickfix/addStarProjections/cast/genericTypeParameter3.kt"); + } + + @TestMetadata("genericTypeParameter4.kt") + public void testGenericTypeParameter4() throws Exception { + runTest("idea/testData/quickfix/addStarProjections/cast/genericTypeParameter4.kt"); + } + + @TestMetadata("genericTypeParameter5.kt") + public void testGenericTypeParameter5() throws Exception { + runTest("idea/testData/quickfix/addStarProjections/cast/genericTypeParameter5.kt"); + } + + @TestMetadata("genericTypeParameter6.kt") + public void testGenericTypeParameter6() throws Exception { + runTest("idea/testData/quickfix/addStarProjections/cast/genericTypeParameter6.kt"); + } + + @TestMetadata("genericTypeParameter7.kt") + public void testGenericTypeParameter7() throws Exception { + runTest("idea/testData/quickfix/addStarProjections/cast/genericTypeParameter7.kt"); + } + @TestMetadata("uncheckedCastOnTypeParameter.kt") public void testUncheckedCastOnTypeParameter() throws Exception { runTest("idea/testData/quickfix/addStarProjections/cast/uncheckedCastOnTypeParameter.kt");