diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/core/quickfix/QuickFixUtil.java b/idea/ide-common/src/org/jetbrains/kotlin/idea/core/quickfix/QuickFixUtil.java index bcb126eb00e..f9e815277dd 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/core/quickfix/QuickFixUtil.java +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/core/quickfix/QuickFixUtil.java @@ -167,7 +167,14 @@ public class QuickFixUtil { public static boolean canFunctionOrGetterReturnExpression(@NotNull KtDeclaration functionOrGetter, @NotNull KtExpression expression) { if (functionOrGetter instanceof KtFunctionLiteral) { KtBlockExpression functionLiteralBody = ((KtFunctionLiteral) functionOrGetter).getBodyExpression(); - PsiElement returnedElement = functionLiteralBody == null ? null : functionLiteralBody.getLastChild(); + PsiElement returnedElement = null; + if (functionLiteralBody != null) { + PsiElement[] children = functionLiteralBody.getChildren(); + int length = children.length; + if (length > 0) { + returnedElement = children[length - 1]; + } + } return returnedElement instanceof KtExpression && canEvaluateTo((KtExpression) returnedElement, expression); } else { diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt index 7f95a697db2..377bffc448b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeFunctionLiteralReturnTypeFix.kt @@ -53,7 +53,8 @@ class ChangeFunctionLiteralReturnTypeFix( val correspondingProperty = PsiTreeUtil.getParentOfType(functionLiteralExpression, KtProperty::class.java) if (correspondingProperty != null && - correspondingProperty.initializer?.let { QuickFixUtil.canEvaluateTo(it, functionLiteralExpression) } ?: true + correspondingProperty.delegate == null && + correspondingProperty.initializer?.let { QuickFixUtil.canEvaluateTo(it, functionLiteralExpression) } != false ) { val correspondingPropertyTypeRef = correspondingProperty.typeReference val propertyType = context.get(BindingContext.TYPE, correspondingPropertyTypeRef) diff --git a/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterTypeWithComment.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterTypeWithComment.kt new file mode 100644 index 00000000000..809791e348e --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterTypeWithComment.kt @@ -0,0 +1,8 @@ +// "Change parameter 'f' type of function 'foo' to '() -> Int'" "true" +fun foo(f: () -> String) {} + +fun test() { + foo { + 1 // comment + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterTypeWithComment.kt.after b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterTypeWithComment.kt.after new file mode 100644 index 00000000000..004c40fdd24 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterTypeWithComment.kt.after @@ -0,0 +1,8 @@ +// "Change parameter 'f' type of function 'foo' to '() -> Int'" "true" +fun foo(f: () -> Int) {} + +fun test() { + foo { + 1 // comment + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterTypeOnPropertyDelegate.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterTypeOnPropertyDelegate.kt new file mode 100644 index 00000000000..06972f67846 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterTypeOnPropertyDelegate.kt @@ -0,0 +1,19 @@ +// "Change parameter 'code' type of primary constructor of class 'TestDelegate' to '() -> Logger'" "true" +// WITH_RUNTIME +import kotlin.reflect.KProperty + +object Test { + val logger by TestDelegate { + Logger(LoggerConfig("From delegate")) + } +} + + +class TestDelegate(val code: () -> LoggerConfig) { + operator fun getValue(kalGlobal: Test, property: KProperty<*>): Any { + return code.invoke() + } +} + +data class LoggerConfig(val name: String) +data class Logger(val loggerConfig: LoggerConfig) \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterTypeOnPropertyDelegate.kt.after b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterTypeOnPropertyDelegate.kt.after new file mode 100644 index 00000000000..17734da4fb2 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterTypeOnPropertyDelegate.kt.after @@ -0,0 +1,19 @@ +// "Change parameter 'code' type of primary constructor of class 'TestDelegate' to '() -> Logger'" "true" +// WITH_RUNTIME +import kotlin.reflect.KProperty + +object Test { + val logger by TestDelegate { + Logger(LoggerConfig("From delegate")) + } +} + + +class TestDelegate(val code: () -> Logger) { + operator fun getValue(kalGlobal: Test, property: KProperty<*>): Any { + return code.invoke() + } +} + +data class LoggerConfig(val name: String) +data class Logger(val loggerConfig: LoggerConfig) \ 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 6c83cb0e7d1..8a0e1430e4f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -14331,6 +14331,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterType4.kt"); } + @TestMetadata("changeFunctionParameterTypeWithComment.kt") + public void testChangeFunctionParameterTypeWithComment() throws Exception { + runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterTypeWithComment.kt"); + } + @TestMetadata("changeParameterTypeLongNameRuntime.kt") public void testChangeParameterTypeLongNameRuntime() throws Exception { runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeParameterTypeLongNameRuntime.kt"); @@ -14341,6 +14346,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterType.kt"); } + @TestMetadata("changePrimaryConstructorParameterTypeOnPropertyDelegate.kt") + public void testChangePrimaryConstructorParameterTypeOnPropertyDelegate() throws Exception { + runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterTypeOnPropertyDelegate.kt"); + } + @TestMetadata("multiFakeOverride.kt") public void testMultiFakeOverride() throws Exception { runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/multiFakeOverride.kt");