From 39f1ef390e9c480db434a20efcd49995c6397cb1 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 19 Jul 2017 20:44:23 +0300 Subject: [PATCH] Do not report "redundant Unit" for generic calls coerced to Unit So #KT-18999 Fixed --- .../RedundantUnitReturnTypeInspection.kt | 29 ++++++++++++++----- .../redundantUnitReturnType/WithLambda.kt | 21 ++++++++++++++ .../inspectionData/expected.xml | 8 +++++ 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 idea/testData/inspections/redundantUnitReturnType/WithLambda.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitReturnTypeInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitReturnTypeInspection.kt index d9426d66680..1ed3fd15481 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitReturnTypeInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitReturnTypeInspection.kt @@ -20,12 +20,14 @@ import com.intellij.codeInspection.IntentionWrapper import com.intellij.codeInspection.ProblemHighlightType import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.PsiElementVisitor -import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeIntention -import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor import org.jetbrains.kotlin.psi.KtCodeFragment import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.KtVisitorVoid +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.typeUtil.isUnit class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() { @@ -34,13 +36,24 @@ class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() { override fun visitNamedFunction(function: KtNamedFunction) { super.visitNamedFunction(function) if (function.containingFile is KtCodeFragment) return - if ((function.descriptor as? FunctionDescriptor)?.returnType?.isUnit() == true) { - function.typeReference?.typeElement?.let { - holder.registerProblem(it, - "Redundant 'Unit' return type", - ProblemHighlightType.LIKE_UNUSED_SYMBOL, - IntentionWrapper(RemoveExplicitTypeIntention(), function.containingKtFile)) + val typeElement = function.typeReference?.typeElement ?: return + val context = function.analyze(BodyResolveMode.PARTIAL) + val descriptor = context[BindingContext.FUNCTION, function] ?: return + if (descriptor.returnType?.isUnit() == true) { + if (!function.hasBlockBody()) { + val bodyExpression = function.bodyExpression + if (bodyExpression != null) { + val resolvedCall = bodyExpression.getResolvedCall(bodyExpression.analyze(BodyResolveMode.PARTIAL)) + if (resolvedCall != null) { + if (resolvedCall.candidateDescriptor.returnType?.isUnit() != true) return + } + } } + + holder.registerProblem(typeElement, + "Redundant 'Unit' return type", + ProblemHighlightType.LIKE_UNUSED_SYMBOL, + IntentionWrapper(RemoveExplicitTypeIntention(), function.containingKtFile)) } } } diff --git a/idea/testData/inspections/redundantUnitReturnType/WithLambda.kt b/idea/testData/inspections/redundantUnitReturnType/WithLambda.kt new file mode 100644 index 00000000000..166766308b9 --- /dev/null +++ b/idea/testData/inspections/redundantUnitReturnType/WithLambda.kt @@ -0,0 +1,21 @@ +fun run(f: () -> T) = f() + +fun foo(): Unit = run { + bar() +} + +fun bar() = 1 + +fun call(f: () -> Unit) = f() + +fun boo(): Unit = call { + baz() +} + +fun baz() {} + +fun T.let(f: (T) -> R) = f(this) + +fun goo(): Unit = 1.let { + bar() +} \ No newline at end of file diff --git a/idea/testData/inspections/redundantUnitReturnType/inspectionData/expected.xml b/idea/testData/inspections/redundantUnitReturnType/inspectionData/expected.xml index 7e0d5e77ca5..c61f7e1895f 100644 --- a/idea/testData/inspections/redundantUnitReturnType/inspectionData/expected.xml +++ b/idea/testData/inspections/redundantUnitReturnType/inspectionData/expected.xml @@ -23,4 +23,12 @@ Redundant 'Unit' return type Redundant Unit return type + + WithLambda.kt + 11 + light_idea_test_case + + Redundant 'Unit' return type + Redundant 'Unit' return type +