Do not report "redundant suspend" on override / open #KT-21938 Fixed

This commit is contained in:
Mikhail Glukhikh
2017-12-21 18:00:42 +03:00
parent 276a6acc3e
commit fc93e7a727
4 changed files with 43 additions and 0 deletions
@@ -22,7 +22,9 @@ import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
import org.jetbrains.kotlin.idea.core.getModalityFromDescriptor
import org.jetbrains.kotlin.idea.highlighter.hasSuspendCalls
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix
@@ -31,6 +33,7 @@ import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtVisitorVoid
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.resolve.BindingContext
class RedundantSuspendModifierInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
@@ -41,8 +44,11 @@ class RedundantSuspendModifierInspection : AbstractKotlinInspection() {
val suspendModifier = function.modifierList?.getModifier(KtTokens.SUSPEND_KEYWORD) ?: return
if (!function.hasBody()) return
if (function.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return
val context = function.analyzeFully()
val descriptor = context[BindingContext.FUNCTION, function] ?: return
if (descriptor.modality == Modality.OPEN) return
if (function.anyDescendantOfType<KtExpression> { it.hasSuspendCalls(context) }) {
return
@@ -40,4 +40,25 @@ class C(val x: Int, val y: Int) {
// Not redundant
suspend fun foo(c1: C, c2: C): Int {
return c1() + c2()
}
interface C {
// Not redundant
suspend fun foo()
}
class D : C {
// Not redundant
override suspend fun foo() {
}
}
open class E {
// Not redundant
open suspend fun bar() {
}
// Not redundant
abstract suspend fun baz()
}
@@ -0,0 +1,10 @@
// PROBLEM: none
interface I {
suspend fun f()
}
class C : I {
override <caret>suspend fun f() {
}
}
@@ -2885,6 +2885,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
doTest(fileName);
}
@TestMetadata("override.kt")
public void testOverride() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSuspend/override.kt");
doTest(fileName);
}
@TestMetadata("setterDelegate.kt")
public void testSetterDelegate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSuspend/setterDelegate.kt");