Files
kotlin-fork/compiler/testData/diagnostics/tests/when/kt4434.kt
T
Evgeniy.Zhelenskiy 931f2eab58 [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
2024-02-21 15:04:03 +00:00

39 lines
833 B
Kotlin
Vendored

// FIR_IDENTICAL
/*
* 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 if (a.length > 0) {
<!NO_ELSE_IN_WHEN!>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
}
}