Report RETURN_NOT_ALLOWED and RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY only on the return with label (KT-13340)
#KT-13340 Fixed
This commit is contained in:
@@ -774,8 +774,8 @@ public interface Errors {
|
||||
|
||||
DiagnosticFactory0<KtSimpleNameExpression> EXPRESSION_EXPECTED_PACKAGE_FOUND = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
DiagnosticFactory0<KtReturnExpression> RETURN_NOT_ALLOWED = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<KtReturnExpression> RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<KtReturnExpression> RETURN_NOT_ALLOWED = DiagnosticFactory0.create(ERROR, PositioningStrategies.RETURN_WITH_LABEL);
|
||||
DiagnosticFactory0<KtReturnExpression> RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY = DiagnosticFactory0.create(ERROR, PositioningStrategies.RETURN_WITH_LABEL);
|
||||
DiagnosticFactory0<KtDeclarationWithBody>
|
||||
NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY = DiagnosticFactory0.create(ERROR, DECLARATION_WITH_BODY);
|
||||
|
||||
|
||||
@@ -477,4 +477,14 @@ object PositioningStrategies {
|
||||
}
|
||||
}
|
||||
|
||||
@JvmField val RETURN_WITH_LABEL: PositioningStrategy<KtReturnExpression> = object: PositioningStrategy<KtReturnExpression>() {
|
||||
override fun mark(element: KtReturnExpression): List<TextRange> {
|
||||
val labeledExpression = element.labeledExpression
|
||||
if (labeledExpression != null) {
|
||||
return markRange(element, labeledExpression)
|
||||
}
|
||||
|
||||
return markElement(element.returnKeyword)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,11 @@
|
||||
package org.jetbrains.kotlin.psi;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.KtNodeTypes;
|
||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||
|
||||
public class KtReturnExpression extends KtExpressionWithLabel implements KtStatementExpression {
|
||||
public KtReturnExpression(@NotNull ASTNode node) {
|
||||
@@ -34,4 +37,15 @@ public class KtReturnExpression extends KtExpressionWithLabel implements KtState
|
||||
public KtExpression getReturnedExpression() {
|
||||
return findChildByClass(KtExpression.class);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public PsiElement getReturnKeyword() {
|
||||
//noinspection ConstantConditions
|
||||
return findChildByType(KtTokens.RETURN_KEYWORD);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PsiElement getLabeledExpression() {
|
||||
return findChildByType(KtNodeTypes.LABEL_QUALIFIER);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ fun test2() : Any = a@ {return@a 1}
|
||||
fun test3() : Any { <!RETURN_TYPE_MISMATCH!>return<!> }
|
||||
fun test4(): ()-> Unit = { <!RETURN_NOT_ALLOWED, RETURN_TYPE_MISMATCH!>return@test4<!> }
|
||||
fun test5(): Any = l@{ return@l }
|
||||
fun test6(): Any = {<!RETURN_NOT_ALLOWED!>return 1<!>}
|
||||
fun test6(): Any = {<!RETURN_NOT_ALLOWED!>return<!> 1}
|
||||
|
||||
fun bbb() {
|
||||
return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>
|
||||
@@ -136,7 +136,7 @@ fun blockNoReturnIfUnitInOneBranch(): Int {
|
||||
fun nonBlockReturnIfEmptyIf(): Int = if (1 < 2) <!TYPE_MISMATCH!>{}<!> else <!TYPE_MISMATCH!>{}<!>
|
||||
fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) <!TYPE_MISMATCH!>{}<!> else 2
|
||||
|
||||
val a = <!RETURN_NOT_ALLOWED!>return 1<!>
|
||||
val a = <!RETURN_NOT_ALLOWED!>return<!> 1
|
||||
|
||||
class A() {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo1(): () -> String = <!RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY!>return<!> { "some long expression "}
|
||||
fun foo2(): () -> String = <!RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY!>return<!UNRESOLVED_REFERENCE!>@label<!><!> { "some long expression "}
|
||||
fun foo3(): () -> String = <!RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY!>return<!><!SYNTAX!>@<!> { "some long expression "}
|
||||
@@ -0,0 +1,5 @@
|
||||
package
|
||||
|
||||
public fun foo1(): () -> kotlin.String
|
||||
public fun foo2(): () -> kotlin.String
|
||||
public fun foo3(): () -> kotlin.String
|
||||
@@ -1,6 +1,6 @@
|
||||
fun find2(): Any? {
|
||||
fun visit(element: Any) {
|
||||
<!RETURN_NOT_ALLOWED!>return@find2 element<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@find2<!> element
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -10,7 +10,7 @@ fun find2(): Any? {
|
||||
fun find(): Any? {
|
||||
object : Any() {
|
||||
fun visit(element: Any) {
|
||||
<!RETURN_NOT_ALLOWED!>return@find element<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@find<!> element
|
||||
}
|
||||
}
|
||||
return null
|
||||
@@ -18,7 +18,7 @@ fun find(): Any? {
|
||||
|
||||
fun find4(): Any? {
|
||||
<!NOT_YET_SUPPORTED_IN_INLINE!>inline fun visit(element: Any) {
|
||||
<!RETURN_NOT_ALLOWED!>return@find4 element<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@find4<!> element
|
||||
}<!>
|
||||
return null
|
||||
}
|
||||
@@ -26,7 +26,7 @@ fun find4(): Any? {
|
||||
fun find3(): Any? {
|
||||
object : Any() {
|
||||
<!NOTHING_TO_INLINE!>inline<!> fun visit(element: Any) {
|
||||
<!RETURN_NOT_ALLOWED!>return@find3 element<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@find3<!> element
|
||||
}
|
||||
}
|
||||
return null
|
||||
|
||||
@@ -12,9 +12,9 @@ fun test() {
|
||||
doSmth(if (true) 3 else return, <!TOO_MANY_ARGUMENTS!>1<!>)
|
||||
}
|
||||
|
||||
val a : Nothing = <!RETURN_NOT_ALLOWED!>return 1<!>
|
||||
val a : Nothing = <!RETURN_NOT_ALLOWED!>return<!> 1
|
||||
|
||||
val b = <!RETURN_NOT_ALLOWED!>return 1<!>
|
||||
val b = <!RETURN_NOT_ALLOWED!>return<!> 1
|
||||
|
||||
val c = doSmth(if (true) 3 else <!RETURN_NOT_ALLOWED!>return<!>)
|
||||
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
|
||||
// KT-5068 Add special error for scala-like syntax 'fun foo(): Int = { 1 }'
|
||||
|
||||
fun test1(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return 1<!> }<!>
|
||||
fun test1(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return<!> 1 }<!>
|
||||
fun test2(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ 1 }<!>
|
||||
val test3: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return 1<!> }<!>
|
||||
val test3: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return<!> 1 }<!>
|
||||
val test4: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ 1 }<!>
|
||||
fun test5(): Int { return <!TYPE_MISMATCH!>{ 1 }<!> }
|
||||
fun test6(): Int = <!TYPE_MISMATCH!>fun (): Int = 1<!>
|
||||
|
||||
fun outer() {
|
||||
fun test1(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return 1<!> }<!>
|
||||
fun test1(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return<!> 1 }<!>
|
||||
fun test2(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ 1 }<!>
|
||||
val test3: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return 1<!> }<!>
|
||||
val test3: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return<!> 1 }<!>
|
||||
val test4: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ 1 }<!>
|
||||
fun test5(): Int { return <!TYPE_MISMATCH!>{ 1 }<!> }
|
||||
fun test6(): Int = <!TYPE_MISMATCH!>fun (): Int = 1<!>
|
||||
}
|
||||
|
||||
class Outer {
|
||||
fun test1(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return 1<!> }<!>
|
||||
fun test1(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return<!> 1 }<!>
|
||||
fun test2(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ 1 }<!>
|
||||
val test3: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return 1<!> }<!>
|
||||
val test3: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return<!> 1 }<!>
|
||||
val test4: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ 1 }<!>
|
||||
fun test5(): Int { return <!TYPE_MISMATCH!>{ 1 }<!> }
|
||||
fun test6(): Int = <!TYPE_MISMATCH!>fun (): Int = 1<!>
|
||||
|
||||
class Nested {
|
||||
fun test1(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return 1<!> }<!>
|
||||
fun test1(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return<!> 1 }<!>
|
||||
fun test2(): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ 1 }<!>
|
||||
val test3: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return 1<!> }<!>
|
||||
val test3: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ <!RETURN_NOT_ALLOWED!>return<!> 1 }<!>
|
||||
val test4: () -> Int = fun (): Int = <!TYPE_MISMATCH_DUE_TO_EQUALS_LAMBDA_IN_FUN!>{ 1 }<!>
|
||||
fun test5(): Int { return <!TYPE_MISMATCH!>{ 1 }<!> }
|
||||
fun test6(): Int = <!TYPE_MISMATCH!>fun (): Int = 1<!>
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
fun f() {
|
||||
foo {
|
||||
bar {
|
||||
<!RETURN_NOT_ALLOWED!>return@foo 1<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@foo<!> 1
|
||||
}
|
||||
return@foo 1
|
||||
}
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ inline fun inlineFun(s: ()->Int) {
|
||||
fun noInlineCall(): String {
|
||||
noInline {
|
||||
if (true) {
|
||||
<!RETURN_NOT_ALLOWED!>return@noInlineCall ""<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@noInlineCall<!> ""
|
||||
}
|
||||
1
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
fun box() : String {
|
||||
test {
|
||||
<!RETURN_NOT_ALLOWED!>return@box "123"<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@box<!> "123"
|
||||
}
|
||||
|
||||
return "OK"
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
fun box() : String {
|
||||
test {
|
||||
<!RETURN_NOT_ALLOWED!>return@box "123"<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@box<!> "123"
|
||||
}
|
||||
|
||||
return "OK"
|
||||
|
||||
Vendored
+1
-1
@@ -7,7 +7,7 @@ inline fun <R> doCallInt(p: () -> R): R {
|
||||
}
|
||||
|
||||
class A {
|
||||
var result: Int = doCallInt { <!RETURN_NOT_ALLOWED!>return this<!> };
|
||||
var result: Int = doCallInt { <!RETURN_NOT_ALLOWED!>return<!> this };
|
||||
|
||||
var field: Int
|
||||
get() {
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ fun nonlocals(b : Boolean) {
|
||||
a@<!UNUSED_LAMBDA_EXPRESSION!>{
|
||||
fun foo() {
|
||||
if (b) {
|
||||
<!RETURN_NOT_ALLOWED!>return@a 1<!> // The label must be resolved, but an error should be reported for a non-local return
|
||||
<!RETURN_NOT_ALLOWED!>return@a<!> 1 // The label must be resolved, but an error should be reported for a non-local return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ fun t2() : String {
|
||||
if (true) {
|
||||
return@l 1
|
||||
}
|
||||
<!RETURN_NOT_ALLOWED!>return "s"<!>
|
||||
<!RETURN_NOT_ALLOWED!>return<!> "s"
|
||||
}
|
||||
return "s"
|
||||
}
|
||||
@@ -32,10 +32,10 @@ fun t3() : String {
|
||||
invoker(
|
||||
l@{
|
||||
if (true) {
|
||||
<!RETURN_NOT_ALLOWED!>return@t3 "1"<!>
|
||||
<!RETURN_NOT_ALLOWED!>return@t3<!> "1"
|
||||
}
|
||||
else {
|
||||
<!RETURN_NOT_ALLOWED!>return <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2<!><!>
|
||||
<!RETURN_NOT_ALLOWED!>return<!> <!CONSTANT_EXPECTED_TYPE_MISMATCH!>2<!>
|
||||
}
|
||||
<!UNREACHABLE_CODE!>return@l 0<!>
|
||||
}
|
||||
@@ -47,7 +47,7 @@ fun t3() : String {
|
||||
)
|
||||
invoker(
|
||||
{
|
||||
<!RETURN_NOT_ALLOWED!>return "0"<!>
|
||||
<!RETURN_NOT_ALLOWED!>return<!> "0"
|
||||
}
|
||||
)
|
||||
return "2"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class A {
|
||||
init {
|
||||
<!RETURN_NOT_ALLOWED!>return<!>
|
||||
<!RETURN_NOT_ALLOWED, UNREACHABLE_CODE!>return 1<!>
|
||||
<!UNREACHABLE_CODE!><!RETURN_NOT_ALLOWED!>return<!> 1<!>
|
||||
}
|
||||
constructor() <!UNREACHABLE_CODE!>{
|
||||
if (1 == 1) {
|
||||
|
||||
@@ -589,6 +589,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ReturnInFunctionWithoutBody.kt")
|
||||
public void testReturnInFunctionWithoutBody() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/ReturnInFunctionWithoutBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("SafeCallInvoke.kt")
|
||||
public void testSafeCallInvoke() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/SafeCallInvoke.kt");
|
||||
|
||||
+1
-1
@@ -131,7 +131,7 @@ fun blockNoReturnIfUnitInOneBranch(): Int {
|
||||
fun nonBlockReturnIfEmptyIf(): Int = if (1 < 2) <error>{}</error> else <error>{}</error>
|
||||
fun nonBlockNoReturnIfUnitInOneBranch(): Int = if (1 < 2) <error>{}</error> else 2
|
||||
|
||||
val a = <error>return 1</error>
|
||||
val a = <error>return</error> 1
|
||||
|
||||
class A() {
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
<error descr="[RETURN_NOT_ALLOWED] 'return' is not allowed here">return a</error>
|
||||
<error descr="[RETURN_NOT_ALLOWED] 'return' is not allowed here">return</error> a
|
||||
@@ -1 +1 @@
|
||||
@file:<error descr="[UNRESOLVED_REFERENCE] Unresolved reference: suppress">suppress</error>(<error descr="[RETURN_NOT_ALLOWED] 'return' is not allowed here"><error descr="[RETURN_NOT_ALLOWED] 'return' is not allowed here">return <error descr="[UNRESOLVED_REFERENCE] Unresolved reference: a"><error descr="[UNRESOLVED_REFERENCE] Unresolved reference: a">a</error></error></error></error>)
|
||||
@file:<error descr="[UNRESOLVED_REFERENCE] Unresolved reference: suppress">suppress</error>(<error descr="[RETURN_NOT_ALLOWED] 'return' is not allowed here"><error descr="[RETURN_NOT_ALLOWED] 'return' is not allowed here">return</error></error> <error descr="[UNRESOLVED_REFERENCE] Unresolved reference: a"><error descr="[UNRESOLVED_REFERENCE] Unresolved reference: a">a</error></error>)
|
||||
Reference in New Issue
Block a user