diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSuspendModifierInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSuspendModifierInspection.kt index 65623868c47..ad2a3675204 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSuspendModifierInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSuspendModifierInspection.kt @@ -17,8 +17,11 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithContent import org.jetbrains.kotlin.idea.highlighter.hasSuspendCalls import org.jetbrains.kotlin.idea.project.languageVersionSettings import org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix +import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtExpression +import org.jetbrains.kotlin.psi.KtNameReferenceExpression +import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.namedFunctionVisitor import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType import org.jetbrains.kotlin.resolve.BindingContext @@ -36,9 +39,7 @@ class RedundantSuspendModifierInspection : AbstractKotlinInspection() { val descriptor = context[BindingContext.FUNCTION, function] ?: return if (descriptor.modality == Modality.OPEN) return - if (function.anyDescendantOfType { it.hasSuspendCalls(context) }) { - return - } + if (function.hasSuspendCalls(context)) return holder.registerProblem( suspendModifier, @@ -51,4 +52,13 @@ class RedundantSuspendModifierInspection : AbstractKotlinInspection() { ) }) } + + private fun KtNamedFunction.hasSuspendCalls(context: BindingContext): Boolean { + return anyDescendantOfType { + if (it is KtNameReferenceExpression && it.getReferencedName() == this.name && it.mainReference.resolve() == this) { + return@anyDescendantOfType false + } + it.hasSuspendCalls(context) + } + } } diff --git a/idea/testData/inspectionsLocal/redundantSuspend/recursiveCall.kt b/idea/testData/inspectionsLocal/redundantSuspend/recursiveCall.kt new file mode 100644 index 00000000000..bac14826441 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantSuspend/recursiveCall.kt @@ -0,0 +1,3 @@ +suspend fun foo() { + foo() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantSuspend/recursiveCall.kt.after b/idea/testData/inspectionsLocal/redundantSuspend/recursiveCall.kt.after new file mode 100644 index 00000000000..1175a9653f4 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantSuspend/recursiveCall.kt.after @@ -0,0 +1,3 @@ +fun foo() { + foo() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 6a07491616d..99298651703 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -8335,6 +8335,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { public void testParenthesized() throws Exception { runTest("idea/testData/inspectionsLocal/redundantSuspend/parenthesized.kt"); } + + @TestMetadata("recursiveCall.kt") + public void testRecursiveCall() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantSuspend/recursiveCall.kt"); + } } @TestMetadata("idea/testData/inspectionsLocal/redundantUnitExpression")