Do not report "redundant Unit" for generic calls coerced to Unit
So #KT-18999 Fixed
This commit is contained in:
+21
-8
@@ -20,12 +20,14 @@ import com.intellij.codeInspection.IntentionWrapper
|
|||||||
import com.intellij.codeInspection.ProblemHighlightType
|
import com.intellij.codeInspection.ProblemHighlightType
|
||||||
import com.intellij.codeInspection.ProblemsHolder
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
import com.intellij.psi.PsiElementVisitor
|
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.intentions.RemoveExplicitTypeIntention
|
||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
|
||||||
import org.jetbrains.kotlin.psi.KtCodeFragment
|
import org.jetbrains.kotlin.psi.KtCodeFragment
|
||||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
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
|
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||||
|
|
||||||
class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() {
|
class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() {
|
||||||
@@ -34,13 +36,24 @@ class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() {
|
|||||||
override fun visitNamedFunction(function: KtNamedFunction) {
|
override fun visitNamedFunction(function: KtNamedFunction) {
|
||||||
super.visitNamedFunction(function)
|
super.visitNamedFunction(function)
|
||||||
if (function.containingFile is KtCodeFragment) return
|
if (function.containingFile is KtCodeFragment) return
|
||||||
if ((function.descriptor as? FunctionDescriptor)?.returnType?.isUnit() == true) {
|
val typeElement = function.typeReference?.typeElement ?: return
|
||||||
function.typeReference?.typeElement?.let {
|
val context = function.analyze(BodyResolveMode.PARTIAL)
|
||||||
holder.registerProblem(it,
|
val descriptor = context[BindingContext.FUNCTION, function] ?: return
|
||||||
"Redundant 'Unit' return type",
|
if (descriptor.returnType?.isUnit() == true) {
|
||||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
if (!function.hasBlockBody()) {
|
||||||
IntentionWrapper(RemoveExplicitTypeIntention(), function.containingKtFile))
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
fun <T> 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, R> T.let(f: (T) -> R) = f(this)
|
||||||
|
|
||||||
|
fun goo(): Unit = 1.let {
|
||||||
|
bar()
|
||||||
|
}
|
||||||
@@ -23,4 +23,12 @@
|
|||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant 'Unit' return type</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant 'Unit' return type</problem_class>
|
||||||
<description>Redundant Unit return type</description>
|
<description>Redundant Unit return type</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
<problem>
|
||||||
|
<file>WithLambda.kt</file>
|
||||||
|
<line>11</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/WithLambda.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant 'Unit' return type</problem_class>
|
||||||
|
<description>Redundant 'Unit' return type</description>
|
||||||
|
</problem>
|
||||||
</problems>
|
</problems>
|
||||||
|
|||||||
Reference in New Issue
Block a user