[inspections] fix false positive "Redundant Unit" inspection in lambda with dynamic return type
^KT-40700 Fixed
This commit is contained in:
+18
-8
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunctionDescript
|
|||||||
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
|
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
|
import org.jetbrains.kotlin.types.isDynamic
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||||
|
|
||||||
class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
||||||
@@ -50,6 +51,7 @@ class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLoc
|
|||||||
if (referenceExpression == parent.lastBlockStatementOrThis()) {
|
if (referenceExpression == parent.lastBlockStatementOrThis()) {
|
||||||
val prev = referenceExpression.previousStatement() ?: return true
|
val prev = referenceExpression.previousStatement() ?: return true
|
||||||
if (prev.isUnitLiteral()) return true
|
if (prev.isUnitLiteral()) return true
|
||||||
|
if (prev is KtDeclaration && isDynamicCall(parent)) return false
|
||||||
val prevType = prev.analyze(BodyResolveMode.PARTIAL).getType(prev)
|
val prevType = prev.analyze(BodyResolveMode.PARTIAL).getType(prev)
|
||||||
if (prevType != null) {
|
if (prevType != null) {
|
||||||
return prevType.isUnit()
|
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 =
|
private fun KtExpression.isUnitLiteral(): Boolean =
|
||||||
StandardNames.FqNames.unit.shortName() == (this as? KtNameReferenceExpression)?.getReferencedNameAsName()
|
StandardNames.FqNames.unit.shortName() == (this as? KtNameReferenceExpression)?.getReferencedNameAsName()
|
||||||
|
|
||||||
private fun KtReturnExpression.expectedReturnType(): KotlinType? {
|
private fun KtReturnExpression.expectedReturnType(): KotlinType? {
|
||||||
val functionDescriptor = getTargetFunctionDescriptor(analyze()) ?: return null
|
val functionDescriptor = getTargetFunctionDescriptor(analyze()) ?: return null
|
||||||
val functionLiteral = DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor) as? KtFunctionLiteral
|
val functionLiteral = DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor) as? KtFunctionLiteral
|
||||||
if (functionLiteral != null) {
|
return if (functionLiteral != null)
|
||||||
val callExpression = functionLiteral.getStrictParentOfType<KtCallExpression>() ?: return null
|
functionLiteral.findLambdaReturnType()
|
||||||
val resolvedCall = callExpression.resolveToCall() ?: return null
|
else
|
||||||
val valueArgument = functionLiteral.getStrictParentOfType<KtValueArgument>() ?: return null
|
functionDescriptor.returnType
|
||||||
val mapping = resolvedCall.getArgumentMapping(valueArgument) as? ArgumentMatch ?: return null
|
}
|
||||||
return mapping.valueParameter.returnType?.arguments?.lastOrNull()?.type
|
|
||||||
}
|
private fun KtFunctionLiteral.findLambdaReturnType(): KotlinType? {
|
||||||
return functionDescriptor.returnType
|
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 {
|
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>
|
||||||
|
}
|
||||||
|
}
|
||||||
+30
@@ -9095,6 +9095,36 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/atLastAfterVal.kt");
|
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")
|
@TestMetadata("labeledReturnAny.kt")
|
||||||
public void testLabeledReturnAny() throws Exception {
|
public void testLabeledReturnAny() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/labeledReturnAny.kt");
|
runTest("idea/testData/inspectionsLocal/redundantUnitExpression/labeledReturnAny.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user