Do not report "redundant suspend" on override / open #KT-21938 Fixed
This commit is contained in:
@@ -22,7 +22,9 @@ import com.intellij.codeInspection.ProblemHighlightType
|
|||||||
import com.intellij.codeInspection.ProblemsHolder
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
import org.jetbrains.kotlin.config.LanguageFeature
|
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.caches.resolve.analyzeFully
|
||||||
|
import org.jetbrains.kotlin.idea.core.getModalityFromDescriptor
|
||||||
import org.jetbrains.kotlin.idea.highlighter.hasSuspendCalls
|
import org.jetbrains.kotlin.idea.highlighter.hasSuspendCalls
|
||||||
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
||||||
import org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix
|
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.KtNamedFunction
|
||||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
|
||||||
class RedundantSuspendModifierInspection : AbstractKotlinInspection() {
|
class RedundantSuspendModifierInspection : AbstractKotlinInspection() {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
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
|
val suspendModifier = function.modifierList?.getModifier(KtTokens.SUSPEND_KEYWORD) ?: return
|
||||||
if (!function.hasBody()) return
|
if (!function.hasBody()) return
|
||||||
|
if (function.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return
|
||||||
|
|
||||||
val context = function.analyzeFully()
|
val context = function.analyzeFully()
|
||||||
|
val descriptor = context[BindingContext.FUNCTION, function] ?: return
|
||||||
|
if (descriptor.modality == Modality.OPEN) return
|
||||||
|
|
||||||
if (function.anyDescendantOfType<KtExpression> { it.hasSuspendCalls(context) }) {
|
if (function.anyDescendantOfType<KtExpression> { it.hasSuspendCalls(context) }) {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -40,4 +40,25 @@ class C(val x: Int, val y: Int) {
|
|||||||
// Not redundant
|
// Not redundant
|
||||||
suspend fun foo(c1: C, c2: C): Int {
|
suspend fun foo(c1: C, c2: C): Int {
|
||||||
return c1() + c2()
|
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);
|
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")
|
@TestMetadata("setterDelegate.kt")
|
||||||
public void testSetterDelegate() throws Exception {
|
public void testSetterDelegate() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSuspend/setterDelegate.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSuspend/setterDelegate.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user