"Redundant suspend modifier": fix false negative with only recursive call

#KT-35829 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-01-13 21:24:16 +09:00
committed by Ilya Kirillov
parent e3871a9961
commit 1f6b7dd88d
4 changed files with 24 additions and 3 deletions
@@ -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<KtExpression> { 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<KtExpression> {
if (it is KtNameReferenceExpression && it.getReferencedName() == this.name && it.mainReference.resolve() == this) {
return@anyDescendantOfType false
}
it.hasSuspendCalls(context)
}
}
}
@@ -0,0 +1,3 @@
<caret>suspend fun foo() {
foo()
}
@@ -0,0 +1,3 @@
fun foo() {
foo()
}
@@ -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")