"Redundant Unit" inspection: Fix for labeled return #KT-23977 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-04-25 07:39:33 +03:00
committed by Mikhail Glukhikh
parent b4853d9293
commit c44cab4565
8 changed files with 84 additions and 1 deletions
@@ -10,10 +10,13 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement
import org.jetbrains.kotlin.js.descriptorUtils.nameIfStandardType
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.lastBlockStatementOrThis
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.typeUtil.isUnit
@@ -36,7 +39,23 @@ class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLoc
private fun KtReferenceExpression.isRedundantUnit(): Boolean {
if (!isUnitLiteral()) return false
val parent = this.parent ?: return false
if (parent is KtReturnExpression) return true
if (parent is KtReturnExpression) {
if (parent.labeledExpression != null) {
val functionLiteral = parent.getStrictParentOfType<KtFunctionLiteral>()
val callExpression = functionLiteral?.getStrictParentOfType<KtCallExpression>()
if (functionLiteral != null && callExpression != null) {
val valueArgument = functionLiteral.getStrictParentOfType<KtValueArgument>()
val index = if (valueArgument != null)
callExpression.valueArguments.indexOf(valueArgument)
else
callExpression.valueArguments.size - 1
val parameter = callExpression.getCallableDescriptor()?.valueParameters?.getOrNull(index)
val returnType = parameter?.returnType?.arguments?.firstOrNull()?.type
if (returnType?.nameIfStandardType == KotlinBuiltIns.FQ_NAMES.any.shortName()) return false
}
}
return true
}
if (parent is KtBlockExpression) {
// Do not report just 'Unit' in function literals (return@label Unit is OK even in literals)
if (parent.getParentOfType<KtFunctionLiteral>(strict = true) != null) return false
@@ -0,0 +1,9 @@
// PROBLEM: none
fun foo(f: () -> Any) {}
fun test() {
foo {
return@foo Unit<caret>
}
}
@@ -0,0 +1,7 @@
// PROBLEM: none
fun foo(f: () -> Unit, g: () -> Any) {}
fun test() {
foo({ return@foo Unit }, { return@foo Unit<caret> })
}
@@ -0,0 +1,7 @@
fun <T> foo(f: () -> T) {}
fun test() {
foo {
return@foo Unit<caret>
}
}
@@ -0,0 +1,7 @@
fun <T> foo(f: () -> T) {}
fun test() {
foo {
return@foo
}
}
@@ -0,0 +1,7 @@
fun foo(f: () -> Unit) {}
fun test() {
foo {
return@foo Unit<caret>
}
}
@@ -0,0 +1,7 @@
fun foo(f: () -> Unit) {}
fun test() {
foo {
return@foo
}
}
@@ -3358,6 +3358,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/atLastAfterVal.kt");
}
@TestMetadata("labeledReturnAny.kt")
public void testLabeledReturnAny() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/labeledReturnAny.kt");
}
@TestMetadata("labeledReturnAnyInValueArgument.kt")
public void testLabeledReturnAnyInValueArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/labeledReturnAnyInValueArgument.kt");
}
@TestMetadata("labeledReturnGenericType.kt")
public void testLabeledReturnGenericType() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/labeledReturnGenericType.kt");
}
@TestMetadata("labeledReturnUnit.kt")
public void testLabeledReturnUnit() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/labeledReturnUnit.kt");
}
@TestMetadata("lambda.kt")
public void testLambda() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/lambda.kt");