KTIJ-29230 [kotlin] Do not report Unit? as redundant return type in K2

`Unit?` return type does not allow empty and
implicit returns, so it cannot be safely removed

^KTIJ-29230 Fixed

GitOrigin-RevId: c9b593e99402cc8b5ce87dcef75fc4a97251b93b
This commit is contained in:
Roman Golyshev
2024-03-27 22:21:20 +00:00
committed by intellij-monorepo-bot
parent fb3c9478be
commit af0c4015f3
4 changed files with 22 additions and 3 deletions
@@ -42,9 +42,14 @@ internal class RedundantUnitReturnTypeInspection :
}
context(KtAnalysisSession)
override fun prepareContext(element: KtNamedFunction): TypeInfo? = when {
element.getFunctionLikeSymbol().returnType.isUnit -> TypeInfo(TypeInfo.UNIT)
else -> null
override fun prepareContext(element: KtNamedFunction): TypeInfo? {
val returnType = element.getFunctionLikeSymbol().returnType
if (!returnType.isMarkedNullable && returnType.isUnit) {
return TypeInfo(TypeInfo.UNIT)
}
return null
}
override fun createQuickFix(
@@ -4918,6 +4918,11 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp
runTest("../../../idea/tests/testData/inspectionsLocal/redundantUnitReturnType/expressionBodyUnitType.kt");
}
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/redundantUnitReturnType/nullableUnit.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/redundantUnitReturnType/simple.kt");
@@ -10742,6 +10742,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
runTest("testData/inspectionsLocal/redundantUnitReturnType/expressionBodyUnitType.kt");
}
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
runTest("testData/inspectionsLocal/redundantUnitReturnType/nullableUnit.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("testData/inspectionsLocal/redundantUnitReturnType/simple.kt");
@@ -0,0 +1,4 @@
// PROBLEM: none
fun test(): <caret>Unit? {
return null
}