Redundant Unit: fix false positive when return type is nullable Unit

#KT-30038 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-02-21 19:40:02 +09:00
committed by Mikhail Glukhikh
parent d67c793a9b
commit df3953f03a
3 changed files with 12 additions and 1 deletions
@@ -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)
@@ -0,0 +1,5 @@
// PROBLEM: none
fun foo(): Unit? {
return Unit<caret>
}
@@ -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")