[kotlin] Provide quick fixes for IMPLICIT_NOTHING_RETURN_TYPE and IMPLICIT_NOTHING_PROPERTY_TYPE

The quick fixes change the type of the enclosing function or property to `Nothing`.

#KTIJ-33891 Fixed

GitOrigin-RevId: e269d45cb9f5fbe30664d1e7d61da86c0ad5e516
This commit is contained in:
Andrey Cherkasov
2025-04-23 00:58:04 +00:00
committed by intellij-monorepo-bot
parent 4898fafd5c
commit 1e7ecbbd9c
9 changed files with 77 additions and 1 deletions
@@ -466,6 +466,25 @@ internal object ChangeTypeQuickFixFactories {
val containerName = parentOfType<KtClassOrObject>()?.nameAsName?.takeUnless { it.isSpecial }
return ChangeTypeFixUtils.functionOrConstructorParameterPresentation(this, containerName?.asString())
}
val implicitNothingPropertyTypeFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.ImplicitNothingPropertyType ->
createImplicitNothingTypeFix(diagnostic.psi as? KtProperty)
}
val implicitNothingReturnTypeFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.ImplicitNothingReturnType ->
createImplicitNothingTypeFix(diagnostic.psi as? KtFunction)
}
private fun createImplicitNothingTypeFix(callable: KtCallableDeclaration?): List<ModCommandAction> {
if (callable == null) return emptyList()
return listOf(
UpdateTypeQuickFix(
callable,
TargetType.ENCLOSING_DECLARATION,
CallableReturnTypeUpdaterUtils.TypeInfo(CallableReturnTypeUpdaterUtils.TypeInfo.NOTHING)
)
)
}
}
@OptIn(KaExperimentalApi::class)
@@ -186,6 +186,8 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
registerFactory(AbstractSuperCallFixFactories.errorFixFactory)
registerFactory(AbstractSuperCallFixFactories.warningFactory)
registerFactory(JavaClassOnCompanionFixFactories.factory)
registerFactory(ChangeTypeQuickFixFactories.implicitNothingReturnTypeFixFactory)
registerFactory(ChangeTypeQuickFixFactories.implicitNothingPropertyTypeFixFactory)
}
private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix {
@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.codeinsights.impl.base
@@ -30,6 +30,8 @@ import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.utils.ChooseValueExpression
import org.jetbrains.kotlin.idea.codeinsights.impl.base.CallableReturnTypeUpdaterUtils.TypeInfo.Companion.createByKtTypes
import org.jetbrains.kotlin.idea.codeinsights.impl.base.CallableReturnTypeUpdaterUtils.TypeInfo.Companion.createTypeByKtType
import org.jetbrains.kotlin.idea.codeinsights.impl.base.CallableReturnTypeUpdaterUtils.setAndShortenTypeReference
import org.jetbrains.kotlin.idea.codeinsights.impl.base.CallableReturnTypeUpdaterUtils.setTypeReference
import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
@@ -340,6 +342,7 @@ object CallableReturnTypeUpdaterUtils {
val UNIT: Type = Type(isUnit = true, isError = false, longTypeRepresentation = "kotlin.Unit", shortTypeRepresentation = "Unit")
val ANY: Type = Type(isUnit = false, isError = false, longTypeRepresentation = "kotlin.Any", shortTypeRepresentation = "Any")
val NOTHING: Type = Type(isUnit = false, isError = false, longTypeRepresentation = "kotlin.Nothing", shortTypeRepresentation = "Nothing")
}
}
@@ -376,11 +376,21 @@ public abstract class K2QuickFixTestGenerated extends AbstractK2QuickFixTest {
runTest("../../../idea/tests/testData/quickfix/changeSignature/changeFunctionLiteralParameters4.kt");
}
@TestMetadata("changeFunctionReturnTypeToNothing.kt")
public void testChangeFunctionReturnTypeToNothing() throws Exception {
runTest("../../../idea/tests/testData/quickfix/changeSignature/changeFunctionReturnTypeToNothing.kt");
}
@TestMetadata("changeParameterType.kt")
public void testChangeParameterType() throws Exception {
runTest("../../../idea/tests/testData/quickfix/changeSignature/changeParameterType.kt");
}
@TestMetadata("changePropertyTypeToNothing.kt")
public void testChangePropertyTypeToNothing() throws Exception {
runTest("../../../idea/tests/testData/quickfix/changeSignature/changePropertyTypeToNothing.kt");
}
@TestMetadata("complexHierarchy.kt")
public void testComplexHierarchy() throws Exception {
runTest("../../../idea/tests/testData/quickfix/changeSignature/complexHierarchy.kt");
@@ -3552,11 +3552,21 @@ public abstract class K1QuickFixTestGenerated extends AbstractK1QuickFixTest {
runTest("testData/quickfix/changeSignature/changeFunctionLiteralParameters4.kt");
}
@TestMetadata("changeFunctionReturnTypeToNothing.kt")
public void testChangeFunctionReturnTypeToNothing() throws Exception {
runTest("testData/quickfix/changeSignature/changeFunctionReturnTypeToNothing.kt");
}
@TestMetadata("changeParameterType.kt")
public void testChangeParameterType() throws Exception {
runTest("testData/quickfix/changeSignature/changeParameterType.kt");
}
@TestMetadata("changePropertyTypeToNothing.kt")
public void testChangePropertyTypeToNothing() throws Exception {
runTest("testData/quickfix/changeSignature/changePropertyTypeToNothing.kt");
}
@TestMetadata("complexHierarchy.kt")
public void testComplexHierarchy() throws Exception {
runTest("testData/quickfix/changeSignature/complexHierarchy.kt");
@@ -0,0 +1,8 @@
// "Change return type of enclosing function 'Test.foo' to 'Nothing'" "true"
class Test {
fun fo<caret>o() = TODO()
}
// IGNORE_K1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeTypeQuickFixFactories$UpdateTypeQuickFix
@@ -0,0 +1,8 @@
// "Change return type of enclosing function 'Test.foo' to 'Nothing'" "true"
class Test {
fun foo(): Nothing = TODO()
}
// IGNORE_K1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeTypeQuickFixFactories$UpdateTypeQuickFix
@@ -0,0 +1,8 @@
// "Change type of enclosing property 'Test.bar' to 'Nothing'" "true"
class Test {
val ba<caret>r = TODO()
}
// IGNORE_K1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeTypeQuickFixFactories$UpdateTypeQuickFix
@@ -0,0 +1,8 @@
// "Change type of enclosing property 'Test.bar' to 'Nothing'" "true"
class Test {
val bar: Nothing = TODO()
}
// IGNORE_K1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeTypeQuickFixFactories$UpdateTypeQuickFix