[FIR] Remove implicit coercion of ifs and whens to Unit

The rule of thumb is the following:
If the `if` and `when` can be successfully replaced with `while`,
then it is used as a statement, otherwise, it is used as an expression.

#KT-59883
This commit is contained in:
Evgeniy.Zhelenskiy
2024-02-20 03:12:56 +01:00
committed by Space Team
parent 2e66954d01
commit 931f2eab58
16 changed files with 256 additions and 68 deletions
-37
View File
@@ -1,37 +0,0 @@
/*
* KOTLIN DIAGNOSTICS SPEC TEST (POSITIVE)
*
* SPEC VERSION: 0.1-152
* PRIMARY LINKS: expressions, when-expression -> paragraph 5 -> sentence 1
* expressions, when-expression, exhaustive-when-expressions -> paragraph 1 -> sentence 1
* expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 1
* expressions, when-expression -> paragraph 6 -> sentence 5
*/
// KT-4434 Missed diagnostic about else branch in when
package test
fun foo(): Int {
val a = "a"
return <!RETURN_TYPE_MISMATCH!>if (a.length > 0) {
when (a) {
"a" -> 1
}
}
else {
3
}<!>
}
fun bar(): Int {
val a = "a"
if (a.length > 0) {
return <!NO_ELSE_IN_WHEN!>when<!> (a) {
"a" -> 1
}
}
else {
return 3
}
}
+1
View File
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
/*
* KOTLIN DIAGNOSTICS SPEC TEST (POSITIVE)
*
@@ -1,13 +0,0 @@
// SKIP_TXT
fun bar(a: String): String {
return <!RETURN_TYPE_MISMATCH!>when {
a.length == 1 -> {
when { // Error in K1, no error in K2
a == "a" -> ""
a == "b" -> ""
}
}
else -> ""
}<!>
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// SKIP_TXT
fun bar(a: String): String {
return when {