Extend NON_MODIFIER_FORM_FOR_BUILT_IN_SUSPEND diagnostic

to include "suspend fun" token sequence as well
This commit is contained in:
Ilmir Usmanov
2021-10-12 01:40:47 +02:00
committed by Space
parent 55ae6d1f3e
commit 559d7015f7
6 changed files with 61 additions and 10 deletions
@@ -34617,6 +34617,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/nonModifierFormForBuiltInRenameOnImport.kt");
}
@Test
@TestMetadata("nonModifierFormForBuiltInWithFun.kt")
public void testNonModifierFormForBuiltInWithFun() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/nonModifierFormForBuiltInWithFun.kt");
}
@Test
@TestMetadata("operators.kt")
public void testOperators() throws Exception {
@@ -34617,6 +34617,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/nonModifierFormForBuiltInRenameOnImport.kt");
}
@Test
@TestMetadata("nonModifierFormForBuiltInWithFun.kt")
public void testNonModifierFormForBuiltInWithFun() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/nonModifierFormForBuiltInWithFun.kt");
}
@Test
@TestMetadata("operators.kt")
public void testOperators() throws Exception {
@@ -34617,6 +34617,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/nonModifierFormForBuiltInRenameOnImport.kt");
}
@Test
@TestMetadata("nonModifierFormForBuiltInWithFun.kt")
public void testNonModifierFormForBuiltInWithFun() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/nonModifierFormForBuiltInWithFun.kt");
}
@Test
@TestMetadata("operators.kt")
public void testOperators() throws Exception {
@@ -7,10 +7,7 @@ package org.jetbrains.kotlin.resolve.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.Call
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.util.isInfixCall
import org.jetbrains.kotlin.resolve.calls.util.isCallableReference
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
@@ -31,22 +28,22 @@ object LambdaWithSuspendModifierCallChecker : CallChecker {
when (descriptor.fqNameOrNull()) {
KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME -> {
if (calleeName != "suspend" || !call.hasFormOfSuspendModifierForLambda() || call.explicitReceiver != null) {
if (calleeName != "suspend" || !call.hasFormOfSuspendModifierForLambdaOrFun() || call.explicitReceiver != null) {
context.trace.report(Errors.NON_MODIFIER_FORM_FOR_BUILT_IN_SUSPEND.on(reportOn))
}
}
else -> {
if ((calleeName == "suspend" || variableCalleeName == "suspend") && call.hasFormOfSuspendModifierForLambda()) {
if ((calleeName == "suspend" || variableCalleeName == "suspend") && call.hasFormOfSuspendModifierForLambdaOrFun()) {
context.trace.report(Errors.MODIFIER_FORM_FOR_NON_BUILT_IN_SUSPEND.on(reportOn))
}
}
}
}
private fun Call.hasFormOfSuspendModifierForLambda() =
private fun Call.hasFormOfSuspendModifierForLambdaOrFun() =
!isCallableReference()
&& typeArguments.isEmpty()
&& (hasNoArgumentListButDanglingLambdas() || isInfixWithRightLambda())
&& (hasNoArgumentListButDanglingLambdas() || isInfixWithRightLambdaOrFun())
private fun Call.referencedName() =
calleeExpression?.safeAs<KtSimpleNameExpression>()?.getReferencedName()
@@ -54,7 +51,7 @@ object LambdaWithSuspendModifierCallChecker : CallChecker {
private fun Call.hasNoArgumentListButDanglingLambdas() =
valueArgumentList?.leftParenthesis == null && functionLiteralArguments.isNotEmpty()
private fun Call.isInfixWithRightLambda() =
private fun Call.isInfixWithRightLambdaOrFun() =
isInfixCall(this)
&& callElement.safeAs<KtBinaryExpression>()?.right is KtLambdaExpression
&& callElement.safeAs<KtBinaryExpression>()?.right.let { it is KtLambdaExpression || it is KtNamedFunction }
}
@@ -0,0 +1,30 @@
// FIR_IDENTICAL
// SKIP_TXT
infix fun Int.suspend(c: () -> Unit) { c() }
fun bar() {
1 <!MODIFIER_FORM_FOR_NON_BUILT_IN_SUSPEND!>suspend<!> fun() {
println()
}
1 <!SYNTAX!>@Ann suspend fun()<!> {
println()
}
1 <!MODIFIER_FORM_FOR_NON_BUILT_IN_SUSPEND!>suspend<!> @Ann fun() {
println()
}
}
@Target(AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
annotation class Ann
fun main(suspend: WLambdaInvoke) {
1 <!MODIFIER_FORM_FOR_NON_BUILT_IN_SUSPEND!>suspend<!> fun() {}
}
class WLambdaInvoke {
operator fun Int.invoke(l: () -> Unit) {}
}
@@ -34713,6 +34713,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/nonModifierFormForBuiltInRenameOnImport.kt");
}
@Test
@TestMetadata("nonModifierFormForBuiltInWithFun.kt")
public void testNonModifierFormForBuiltInWithFun() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/nonModifierFormForBuiltInWithFun.kt");
}
@Test
@TestMetadata("operators.kt")
public void testOperators() throws Exception {