diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt index 7499b2cc499..3733c4be830 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt @@ -23,7 +23,9 @@ import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiDocumentManager import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny @@ -41,7 +43,6 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.* -import org.jetbrains.kotlin.types.typeUtil.containsTypeAliases import org.jetbrains.kotlin.utils.ifEmpty class SpecifyTypeExplicitlyIntention : @@ -114,7 +115,17 @@ class SpecifyTypeExplicitlyIntention : val resolutionFacade = contextElement.getResolutionFacade() val bindingContext = resolutionFacade.analyze(contextElement, BodyResolveMode.PARTIAL) val scope = contextElement.getResolutionScope(bindingContext, resolutionFacade) - val types = with (exprType.getResolvableApproximations(scope, true).toList()) { + + var checkTypeParameters = true + val descriptor = exprType.constructor.declarationDescriptor + if (descriptor != null && descriptor is TypeParameterDescriptor) { + val owner = descriptor.containingDeclaration + if (owner is FunctionDescriptor && owner.typeParameters.contains(descriptor)) { + checkTypeParameters = false + } + } + + val types = with (exprType.getResolvableApproximations(scope, checkTypeParameters).toList()) { when { exprType.isNullabilityFlexible() -> flatMap { listOf(TypeUtils.makeNotNullable(it), TypeUtils.makeNullable(it)) diff --git a/idea/testData/intentions/specifyTypeExplicitly/genericClass.kt b/idea/testData/intentions/specifyTypeExplicitly/genericClass.kt new file mode 100644 index 00000000000..0609212f962 --- /dev/null +++ b/idea/testData/intentions/specifyTypeExplicitly/genericClass.kt @@ -0,0 +1,5 @@ +fun generic1(): T = null!! + +class My { + fun foo() = generic1() +} \ No newline at end of file diff --git a/idea/testData/intentions/specifyTypeExplicitly/genericClass.kt.after b/idea/testData/intentions/specifyTypeExplicitly/genericClass.kt.after new file mode 100644 index 00000000000..04031a82362 --- /dev/null +++ b/idea/testData/intentions/specifyTypeExplicitly/genericClass.kt.after @@ -0,0 +1,5 @@ +fun generic1(): T = null!! + +class My { + fun foo(): T = generic1() +} \ No newline at end of file diff --git a/idea/testData/intentions/specifyTypeExplicitly/genericFunction.kt b/idea/testData/intentions/specifyTypeExplicitly/genericFunction.kt new file mode 100644 index 00000000000..8718827a4fa --- /dev/null +++ b/idea/testData/intentions/specifyTypeExplicitly/genericFunction.kt @@ -0,0 +1,2 @@ +fun generic1(): T? = null +fun foo() = generic1() \ No newline at end of file diff --git a/idea/testData/intentions/specifyTypeExplicitly/genericFunction.kt.after b/idea/testData/intentions/specifyTypeExplicitly/genericFunction.kt.after new file mode 100644 index 00000000000..81fc19d1557 --- /dev/null +++ b/idea/testData/intentions/specifyTypeExplicitly/genericFunction.kt.after @@ -0,0 +1,2 @@ +fun generic1(): T? = null +fun foo(): T? = generic1() \ 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 54037b30d2d..19affd44868 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -14609,6 +14609,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("genericClass.kt") + public void testGenericClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/genericClass.kt"); + doTest(fileName); + } + + @TestMetadata("genericFunction.kt") + public void testGenericFunction() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/genericFunction.kt"); + doTest(fileName); + } + @TestMetadata("lambdaParam.kt") public void testLambdaParam() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/lambdaParam.kt");