[inspections] fix false positive "Redundant Unit" inspection in lambda with dynamic return type

^KT-40700 Fixed
This commit is contained in:
Dmitry Gridin
2020-10-07 15:15:34 +07:00
parent 26d03295cf
commit 9a0c3c47c8
10 changed files with 138 additions and 8 deletions
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunctionDescript
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isDynamic
import org.jetbrains.kotlin.types.typeUtil.isUnit
class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
@@ -50,6 +51,7 @@ class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLoc
if (referenceExpression == parent.lastBlockStatementOrThis()) {
val prev = referenceExpression.previousStatement() ?: return true
if (prev.isUnitLiteral()) return true
if (prev is KtDeclaration && isDynamicCall(parent)) return false
val prevType = prev.analyze(BodyResolveMode.PARTIAL).getType(prev)
if (prevType != null) {
return prevType.isUnit()
@@ -72,20 +74,28 @@ class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLoc
}
}
private fun isDynamicCall(parent: KtBlockExpression): Boolean = parent.getStrictParentOfType<KtFunctionLiteral>()
?.findLambdaReturnType()
?.isDynamic() == true
private fun KtExpression.isUnitLiteral(): Boolean =
StandardNames.FqNames.unit.shortName() == (this as? KtNameReferenceExpression)?.getReferencedNameAsName()
private fun KtReturnExpression.expectedReturnType(): KotlinType? {
val functionDescriptor = getTargetFunctionDescriptor(analyze()) ?: return null
val functionLiteral = DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor) as? KtFunctionLiteral
if (functionLiteral != null) {
val callExpression = functionLiteral.getStrictParentOfType<KtCallExpression>() ?: return null
val resolvedCall = callExpression.resolveToCall() ?: return null
val valueArgument = functionLiteral.getStrictParentOfType<KtValueArgument>() ?: return null
val mapping = resolvedCall.getArgumentMapping(valueArgument) as? ArgumentMatch ?: return null
return mapping.valueParameter.returnType?.arguments?.lastOrNull()?.type
}
return functionDescriptor.returnType
return if (functionLiteral != null)
functionLiteral.findLambdaReturnType()
else
functionDescriptor.returnType
}
private fun KtFunctionLiteral.findLambdaReturnType(): KotlinType? {
val callExpression = getStrictParentOfType<KtCallExpression>() ?: return null
val resolvedCall = callExpression.resolveToCall() ?: return null
val valueArgument = getStrictParentOfType<KtValueArgument>() ?: return null
val mapping = resolvedCall.getArgumentMapping(valueArgument) as? ArgumentMatch ?: return null
return mapping.valueParameter.returnType?.arguments?.lastOrNull()?.type
}
private class RemoveRedundantUnitFix : LocalQuickFix {
@@ -0,0 +1,10 @@
// PROBLEM: none
// ERROR: Unsupported [Dynamic types are not supported in this context]
fun foo() {
fun bar(f: () -> dynamic): Unit {}
bar {
val a = 1
Unit<caret>
}
}
@@ -0,0 +1,10 @@
// PROBLEM: none
// ERROR: Unsupported [Dynamic types are not supported in this context]
fun foo() {
fun bar(f: () -> dynamic): Unit {}
bar({
val a = 1
Unit<caret>
})
}
@@ -0,0 +1,14 @@
// PROBLEM: none
// ERROR: Unsupported [Dynamic types are not supported in this context]
// ERROR: Unsupported [Dynamic types are not supported in this context]
fun foo() {
fun bar(c: () -> dynamic, f: () -> dynamic): Unit {}
bar({
val a = 1
Unit
}) {
val a = 1
Unit<caret>
}
}
@@ -0,0 +1,12 @@
// ERROR: Unsupported [Dynamic types are not supported in this context]
fun foo() {
fun <T> bar(c: () -> dynamic, f: () -> T): Unit {}
bar({
val a = 1
Unit
}) {
val a = 1
Unit<caret>
}
}
@@ -0,0 +1,11 @@
// ERROR: Unsupported [Dynamic types are not supported in this context]
fun foo() {
fun <T> bar(c: () -> dynamic, f: () -> T): Unit {}
bar({
val a = 1
Unit
}) {
val a = 1
}
}
@@ -0,0 +1,12 @@
// ERROR: Unsupported [Dynamic types are not supported in this context]
fun foo() {
fun <T> bar(c: () -> T, f: () -> dynamic): Unit {}
bar({
val a = 1
Unit<caret>
}) {
val a = 1
Unit
}
}
@@ -0,0 +1,11 @@
// ERROR: Unsupported [Dynamic types are not supported in this context]
fun foo() {
fun <T> bar(c: () -> T, f: () -> dynamic): Unit {}
bar({
val a = 1
}) {
val a = 1
Unit
}
}
@@ -0,0 +1,10 @@
// PROBLEM: none
// ERROR: Unsupported [Dynamic types are not supported in this context]
fun foo() {
fun bar(f: (Int) -> dynamic): Unit {}
bar {
val a = 1
Unit<caret>
}
}
@@ -9095,6 +9095,36 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/atLastAfterVal.kt");
}
@TestMetadata("dynamic.kt")
public void testDynamic() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/dynamic.kt");
}
@TestMetadata("dynamic2.kt")
public void testDynamic2() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/dynamic2.kt");
}
@TestMetadata("dynamic3.kt")
public void testDynamic3() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/dynamic3.kt");
}
@TestMetadata("dynamic4.kt")
public void testDynamic4() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/dynamic4.kt");
}
@TestMetadata("dynamic5.kt")
public void testDynamic5() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/dynamic5.kt");
}
@TestMetadata("dynamic6.kt")
public void testDynamic6() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/dynamic6.kt");
}
@TestMetadata("labeledReturnAny.kt")
public void testLabeledReturnAny() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/labeledReturnAny.kt");