Redundant Unit: fix false positive when return type is nullable Unit
#KT-30038 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
d67c793a9b
commit
df3953f03a
@@ -44,7 +44,8 @@ private fun KtReferenceExpression.isRedundantUnit(): Boolean {
|
||||
if (!isUnitLiteral()) return false
|
||||
val parent = this.parent ?: return false
|
||||
if (parent is KtReturnExpression) {
|
||||
return parent.expectedReturnType()?.nameIfStandardType != KotlinBuiltIns.FQ_NAMES.any.shortName()
|
||||
val expectedReturnType = parent.expectedReturnType() ?: return false
|
||||
return expectedReturnType.nameIfStandardType != KotlinBuiltIns.FQ_NAMES.any.shortName() && !expectedReturnType.isMarkedNullable
|
||||
}
|
||||
if (parent is KtBlockExpression) {
|
||||
// Do not report just 'Unit' in function literals (return@label Unit is OK even in literals)
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(): Unit? {
|
||||
return Unit<caret>
|
||||
}
|
||||
+5
@@ -5733,6 +5733,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
public void testReturnAsNullableAny() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/returnAsNullableAny.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("returnAsNullableUnit.kt")
|
||||
public void testReturnAsNullableUnit() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/returnAsNullableUnit.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/redundantVisibilityModifier")
|
||||
|
||||
Reference in New Issue
Block a user