'Else' is not required in 'when' if Unit is expected

This commit is contained in:
Andrey Breslav
2013-09-12 22:04:34 +02:00
parent a76f1c1db5
commit c017858f77
17 changed files with 170 additions and 14 deletions
@@ -0,0 +1,7 @@
fun foo(x: Int) {
val y: Unit = when (x) {
2 -> {}
3 -> {}
}
return y
}
@@ -0,0 +1,6 @@
fun foo(x: Int): Any {
val v = <!NO_ELSE_IN_WHEN!>when<!> (x) {
2 -> 0
}
return v
}
@@ -0,0 +1,11 @@
fun foo(x: Int) {
r {
when (x) {
2 -> 0
}
}
}
fun r(f: () -> Unit) {
f()
}
@@ -0,0 +1,11 @@
fun foo(x: Int) {
r {
<!NO_ELSE_IN_WHEN!>when<!> (x) {
2 -> 0
}
}
}
fun r(f: () -> Int) {
f()
}
@@ -0,0 +1,5 @@
fun foo(x: Int): Any {
return <!NO_ELSE_IN_WHEN!>when<!> (x) {
2 -> 0
}
}
@@ -0,0 +1,6 @@
fun foo(x: Int) {
return when (x) {
2 -> {}
3 -> {}
}
}
@@ -0,0 +1,6 @@
fun foo(x: Int) {
when (x) {
2 -> {}
3 -> {}
}
}
@@ -0,0 +1,44 @@
fun Int.foo() : Boolean = true
fun foo() : Int {
val s = ""
val x = 1
when (x) {
is <!INCOMPATIBLE_TYPES!>String<!> -> 1
!is Int -> 1
is Any<!USELESS_NULLABLE_CHECK!>?<!> -> 1
<!INCOMPATIBLE_TYPES!>s<!> -> 1
1 -> 1
1 + <!UNRESOLVED_REFERENCE!>a<!> -> 1
in 1..<!UNRESOLVED_REFERENCE!>a<!> -> 1
!in 1..<!UNRESOLVED_REFERENCE!>a<!> -> 1
else -> 1
}
return 0
}
val _type_test : Int = foo() // this is needed to ensure the inferred return type of foo()
fun test() {
val x = 1;
val s = "";
when (x) {
<!INCOMPATIBLE_TYPES!>s<!> -> 1
<!INCOMPATIBLE_TYPES!>""<!> -> 1
x -> 1
1 -> 1
}
val z = 1
when (z) {
<!ELSE_MISPLACED_IN_WHEN!>else<!> -> 1
<!UNREACHABLE_CODE!>1 -> 2<!>
}
when (z) {
else -> 1
}
}