From 3221fbdca9e85ebe7bfed066eb999e1c80255f2c Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 11 Apr 2018 16:27:03 +0300 Subject: [PATCH] Don't "Remove explicit type" for callable with function initializer So #KT-12168 Fixed --- .../intentions/RemoveExplicitTypeIntention.kt | 21 +++++- .../anonymousFunctionInitializer.kt | 1 + .../anonymousFunctionInitializer.kt.after | 1 + .../anonymousFunctionInitializer2.kt | 1 + .../anonymousFunctionInitializer2.kt.after | 1 + .../anonymousFunctionInitializer3.kt | 2 + .../lambdaExpressionBody.kt | 2 + .../removeExplicitType/lambdaInitializer.kt | 1 + .../lambdaInitializer.kt.after | 1 + .../removeExplicitType/lambdaInitializer2.kt | 1 + .../lambdaInitializer2.kt.after | 1 + .../removeExplicitType/lambdaInitializer3.kt | 2 + .../removeExplicitType/lambdaInitializer4.kt | 2 + .../removeExplicitType/lambdaInitializer5.kt | 3 + .../intentions/IntentionTestGenerated.java | 64 ++++++++++++++++--- 15 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt create mode 100644 idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt.after create mode 100644 idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt create mode 100644 idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt.after create mode 100644 idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer3.kt create mode 100644 idea/testData/intentions/removeExplicitType/lambdaExpressionBody.kt create mode 100644 idea/testData/intentions/removeExplicitType/lambdaInitializer.kt create mode 100644 idea/testData/intentions/removeExplicitType/lambdaInitializer.kt.after create mode 100644 idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt create mode 100644 idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt.after create mode 100644 idea/testData/intentions/removeExplicitType/lambdaInitializer3.kt create mode 100644 idea/testData/intentions/removeExplicitType/lambdaInitializer4.kt create mode 100644 idea/testData/intentions/removeExplicitType/lambdaInitializer5.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt index b61107f3cbf..e9accef4a2d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt @@ -49,10 +49,25 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention initializer.valueParameters + is KtNamedFunction -> initializer.valueParameters + else -> emptyList() + } + if (valueParameters.isEmpty() || valueParameters.any { it.typeReference == null }) return null + } } return when { diff --git a/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt new file mode 100644 index 00000000000..9c5ba5fc023 --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt @@ -0,0 +1 @@ +val foo: () -> String = { "" } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt.after b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt.after new file mode 100644 index 00000000000..ea5134b5782 --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt.after @@ -0,0 +1 @@ +val foo = { "" } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt new file mode 100644 index 00000000000..cc732e6bd5c --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt @@ -0,0 +1 @@ +val foo: (Int, Int) -> String = fun(i: Int, j: Int): String { return (i + j).toString() } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt.after b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt.after new file mode 100644 index 00000000000..830366a435b --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt.after @@ -0,0 +1 @@ +val foo = fun(i: Int, j: Int): String { return (i + j).toString() } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer3.kt b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer3.kt new file mode 100644 index 00000000000..2c476fb4375 --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer3.kt @@ -0,0 +1,2 @@ +// IS_APPLICABLE: false +val foo: (Int, Int) -> String = fun(i, j: Int): String { return (i + j).toString() } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/lambdaExpressionBody.kt b/idea/testData/intentions/removeExplicitType/lambdaExpressionBody.kt new file mode 100644 index 00000000000..9668e396f40 --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/lambdaExpressionBody.kt @@ -0,0 +1,2 @@ +// IS_APPLICABLE: false +fun foo(): (Int) -> String = { it.toString() } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/lambdaInitializer.kt b/idea/testData/intentions/removeExplicitType/lambdaInitializer.kt new file mode 100644 index 00000000000..1a3bf7d7de0 --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/lambdaInitializer.kt @@ -0,0 +1 @@ +val foo: () -> String = fun(): String { return "" } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/lambdaInitializer.kt.after b/idea/testData/intentions/removeExplicitType/lambdaInitializer.kt.after new file mode 100644 index 00000000000..4e518fe5b08 --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/lambdaInitializer.kt.after @@ -0,0 +1 @@ +val foo = fun(): String { return "" } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt b/idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt new file mode 100644 index 00000000000..bfa175aca5a --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt @@ -0,0 +1 @@ +val foo: (Int, Int) -> String = { i: Int, j: Int -> (i + j).toString() } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt.after b/idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt.after new file mode 100644 index 00000000000..9a6f10ce16c --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt.after @@ -0,0 +1 @@ +val foo = { i: Int, j: Int -> (i + j).toString() } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/lambdaInitializer3.kt b/idea/testData/intentions/removeExplicitType/lambdaInitializer3.kt new file mode 100644 index 00000000000..2d170486f33 --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/lambdaInitializer3.kt @@ -0,0 +1,2 @@ +// IS_APPLICABLE: false +val foo: (Int, Int) -> String = { i, j: Int -> (i + j).toString() } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/lambdaInitializer4.kt b/idea/testData/intentions/removeExplicitType/lambdaInitializer4.kt new file mode 100644 index 00000000000..6447c9ad5bb --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/lambdaInitializer4.kt @@ -0,0 +1,2 @@ +// IS_APPLICABLE: false +val foo: (Int) -> String = { it.toString() } \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/lambdaInitializer5.kt b/idea/testData/intentions/removeExplicitType/lambdaInitializer5.kt new file mode 100644 index 00000000000..62171cfbada --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/lambdaInitializer5.kt @@ -0,0 +1,3 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +val foo: (Pair) -> String = { (i: Int, j: Int) -> (i + j).toString() } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index a656bbeb857..99ea7d3bf10 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -8391,38 +8391,37 @@ public class IntentionTestGenerated extends AbstractIntentionTest { @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class IndentRawString extends AbstractIntentionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + public void testAllFilesPresentInIndentRawString() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/indentRawString"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } @TestMetadata("hasIndent.kt") public void testHasIndent() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/hasIndent.kt"); - doTest(fileName); + runTest("idea/testData/intentions/indentRawString/hasIndent.kt"); } @TestMetadata("notRawString.kt") public void testNotRawString() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/notRawString.kt"); - doTest(fileName); + runTest("idea/testData/intentions/indentRawString/notRawString.kt"); } @TestMetadata("receiver.kt") public void testReceiver() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/receiver.kt"); - doTest(fileName); + runTest("idea/testData/intentions/indentRawString/receiver.kt"); } @TestMetadata("simple.kt") public void testSimple() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/simple.kt"); - doTest(fileName); + runTest("idea/testData/intentions/indentRawString/simple.kt"); } @TestMetadata("singleLine.kt") public void testSingleLine() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/singleLine.kt"); - doTest(fileName); + runTest("idea/testData/intentions/indentRawString/singleLine.kt"); } } @@ -12476,6 +12475,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeExplicitType"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("anonymousFunctionInitializer.kt") + public void testAnonymousFunctionInitializer() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer.kt"); + } + + @TestMetadata("anonymousFunctionInitializer2.kt") + public void testAnonymousFunctionInitializer2() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer2.kt"); + } + + @TestMetadata("anonymousFunctionInitializer3.kt") + public void testAnonymousFunctionInitializer3() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/anonymousFunctionInitializer3.kt"); + } + @TestMetadata("funNoBody.kt") public void testFunNoBody() throws Exception { runTest("idea/testData/intentions/removeExplicitType/funNoBody.kt"); @@ -12491,6 +12505,36 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/removeExplicitType/hasAnnotation.kt"); } + @TestMetadata("lambdaExpressionBody.kt") + public void testLambdaExpressionBody() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/lambdaExpressionBody.kt"); + } + + @TestMetadata("lambdaInitializer.kt") + public void testLambdaInitializer() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/lambdaInitializer.kt"); + } + + @TestMetadata("lambdaInitializer2.kt") + public void testLambdaInitializer2() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/lambdaInitializer2.kt"); + } + + @TestMetadata("lambdaInitializer3.kt") + public void testLambdaInitializer3() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/lambdaInitializer3.kt"); + } + + @TestMetadata("lambdaInitializer4.kt") + public void testLambdaInitializer4() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/lambdaInitializer4.kt"); + } + + @TestMetadata("lambdaInitializer5.kt") + public void testLambdaInitializer5() throws Exception { + runTest("idea/testData/intentions/removeExplicitType/lambdaInitializer5.kt"); + } + @TestMetadata("notOnParameterOfFunctionType.kt") public void testNotOnParameterOfFunctionType() throws Exception { runTest("idea/testData/intentions/removeExplicitType/notOnParameterOfFunctionType.kt");