From d0ed76ebc84f5e58afb5914f2a48b7a33f2a75d2 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Wed, 26 Sep 2018 14:41:37 +0300 Subject: [PATCH] 183: Fix exception about unmapped lambda argument from IDE inspection Intention: obtain lambda argument for some parent call, then get that call (for the lambda argument) and map the argument to the parameter. Before the fix, we were getting call for inner element, not for the lambda argument and as a result, there were exceptions. #KT-26873 Fixed --- .../CoroutineNonBlockingContextChecker.kt.183 | 2 +- .../NestedFunctionsInsideSuspendLambda.kt.183 | 11 +++++++++++ .../CoroutineNonBlockingontextDetectionTest.kt.183 | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 idea/testData/inspections/blockingCallsDetection/NestedFunctionsInsideSuspendLambda.kt.183 diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/blockingCallsDetection/CoroutineNonBlockingContextChecker.kt.183 b/idea/src/org/jetbrains/kotlin/idea/inspections/blockingCallsDetection/CoroutineNonBlockingContextChecker.kt.183 index 4fb01f7e247..63c07dffaa7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/blockingCallsDetection/CoroutineNonBlockingContextChecker.kt.183 +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/blockingCallsDetection/CoroutineNonBlockingContextChecker.kt.183 @@ -44,7 +44,7 @@ class CoroutineNonBlockingContextChecker : NonBlockingContextChecker { .firstOrNull { it is KtLambdaExpression && it.analyze().get(BindingContext.LAMBDA_INVOCATIONS, it) == null } val containingArgument = PsiTreeUtil.getParentOfType(containingLambda, KtValueArgument::class.java) if (containingArgument != null) { - val callExpression = PsiTreeUtil.getParentOfType(element, KtCallExpression::class.java) ?: return false + val callExpression = PsiTreeUtil.getParentOfType(containingArgument, KtCallExpression::class.java) ?: return false val call = callExpression.resolveToCall(BodyResolveMode.FULL) ?: return false val hasBlockingAnnotation = call.getFirstArgument()?.resolveToCall() diff --git a/idea/testData/inspections/blockingCallsDetection/NestedFunctionsInsideSuspendLambda.kt.183 b/idea/testData/inspections/blockingCallsDetection/NestedFunctionsInsideSuspendLambda.kt.183 new file mode 100644 index 00000000000..ca3c749a015 --- /dev/null +++ b/idea/testData/inspections/blockingCallsDetection/NestedFunctionsInsideSuspendLambda.kt.183 @@ -0,0 +1,11 @@ +fun runBlocking(block: suspend CoroutineScope.() -> T): T = TODO() + +class CoroutineScope + +fun test() { + runBlocking { + repeat(1) { + java.lang.Thread.sleep(2) + } + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/CoroutineNonBlockingontextDetectionTest.kt.183 b/idea/tests/org/jetbrains/kotlin/idea/inspections/CoroutineNonBlockingontextDetectionTest.kt.183 index 04ac6cac445..a5c8a189bda 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/CoroutineNonBlockingontextDetectionTest.kt.183 +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/CoroutineNonBlockingontextDetectionTest.kt.183 @@ -38,4 +38,9 @@ class CoroutineNonBlockingontextDetectionTest: KotlinLightCodeInsightFixtureTest myFixture.configureByFile("LambdaReceiverTypeCheck.kt") myFixture.testHighlighting(true, false, false, "LambdaReceiverTypeCheck.kt") } + + fun testNestedFunctionsInsideSuspendLambda() { + myFixture.configureByFile("NestedFunctionsInsideSuspendLambda.kt") + myFixture.testHighlighting(true, false, false, "NestedFunctionsInsideSuspendLambda.kt") + } } \ No newline at end of file