Don't "Remove explicit type" for callable with function initializer

So #KT-12168 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-04-11 16:27:03 +03:00
committed by Mikhail Glukhikh
parent 5204c73bec
commit 3221fbdca9
15 changed files with 91 additions and 13 deletions
@@ -49,10 +49,25 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclar
if (element.isSetterParameter) return typeReference.textRange
}
val initializer = (element as? KtDeclarationWithInitializer)?.initializer
if (element !is KtProperty && element !is KtNamedFunction) return null
(element as? KtNamedFunction)?.let {
if (it.hasBlockBody() && (element.descriptor as? FunctionDescriptor)?.returnType?.isUnit()?.not() != false) return null
if (element is KtNamedFunction
&& element.hasBlockBody()
&& (element.descriptor as? FunctionDescriptor)?.returnType?.isUnit()?.not() != false
) return null
val initializer = (element as? KtDeclarationWithInitializer)?.initializer
if (initializer is KtLambdaExpression || initializer is KtNamedFunction) {
val functionType = element.typeReference?.typeElement as? KtFunctionType
if (functionType?.parameters?.isNotEmpty() == true) {
val valueParameters = when (initializer) {
is KtLambdaExpression -> initializer.valueParameters
is KtNamedFunction -> initializer.valueParameters
else -> emptyList()
}
if (valueParameters.isEmpty() || valueParameters.any { it.typeReference == null }) return null
}
}
return when {
@@ -0,0 +1 @@
val foo: <caret>() -> String = { "" }
@@ -0,0 +1 @@
val foo = { "" }
@@ -0,0 +1 @@
val foo: <caret>(Int, Int) -> String = fun(i: Int, j: Int): String { return (i + j).toString() }
@@ -0,0 +1 @@
val foo = fun(i: Int, j: Int): String { return (i + j).toString() }
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val foo: <caret>(Int, Int) -> String = fun(i, j: Int): String { return (i + j).toString() }
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
fun foo(): <caret>(Int) -> String = { it.toString() }
@@ -0,0 +1 @@
val foo: <caret>() -> String = fun(): String { return "" }
@@ -0,0 +1 @@
val foo = fun(): String { return "" }
@@ -0,0 +1 @@
val foo: <caret>(Int, Int) -> String = { i: Int, j: Int -> (i + j).toString() }
@@ -0,0 +1 @@
val foo = { i: Int, j: Int -> (i + j).toString() }
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val foo: <caret>(Int, Int) -> String = { i, j: Int -> (i + j).toString() }
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val foo: <caret>(Int) -> String = { it.toString() }
@@ -0,0 +1,3 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
val foo: <caret>(Pair<Int, Int>) -> String = { (i: Int, j: Int) -> (i + j).toString() }
@@ -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");