Skip to content

Commit 1a1fe34

Browse files
committed
add patmat exhaustivity test examples
1 parent 74fb8f5 commit 1a1fe34

File tree

4 files changed

+1010
-0
lines changed

4 files changed

+1010
-0
lines changed

examples/patmat-adt.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
object PatmatADT {
2+
abstract sealed class Odd(x: Odd)
3+
4+
case class Good(x: Odd) extends Odd(x)
5+
case class Bad(x: Odd) extends Odd(x)
6+
7+
def foo1a(x: Odd) = x match { // warning: Good(_: Bad), Bad(_: Good)
8+
case Good(_: Good) => false
9+
case Bad(_: Bad) => false
10+
}
11+
12+
def foo1b(x: Odd) = x match {
13+
case Good(_: Good) => false
14+
case Bad(_: Bad) => false
15+
case Good(_: Bad) => false
16+
case Bad(_: Good) => false
17+
}
18+
19+
def foo2(x: Option[Int]) = x match { // warning: Some(_: Int)
20+
case Some(_: Double) => true
21+
case None => true
22+
}
23+
24+
def foo3a[T](x: Option[T]) = (x, x) match { // warning: (Some(_), Some(_)), (None, Some(_))
25+
case (Some(_), None) => true
26+
case (None, None) => true
27+
}
28+
29+
def foo3b[T](x: Option[T]) = (x, x) match { // warning: (Some(_), Some(_)), (None, None)
30+
case (Some(_), None) => true
31+
case (None, Some(_)) => true
32+
}
33+
}

examples/patmat-ortype.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
object PatmatOrType {
2+
3+
def foo1(x: Int | Double) = x match {
4+
case _: Int => true
5+
case _: Double => true
6+
}
7+
8+
def foo2a(x: Int | Double | String) = x match { // _: String not matched
9+
case _: Int => true
10+
case _: Double => true
11+
}
12+
13+
def foo2b(x: Int | Double | String) = x match {
14+
case _: Int => true
15+
case _: (Double | String) => true
16+
}
17+
18+
def foo3(x: Option[Int | Double | String]) = x match { // warning: None, Some(_: String) not matched
19+
case Some(_: Int) => true
20+
case Some(_: Double) => true
21+
}
22+
23+
def foo4(x: Option[Int | Double | String]) = x match {
24+
case Some(_: Int) => true
25+
case Some(_: Double) => true
26+
case Some(_: String) => true
27+
case None => false
28+
}
29+
30+
def foo5a(x: Option[Int | Double | String]) = x match {
31+
case Some(_: (Int | Double)) => true
32+
case Some(_: String) => true
33+
case None => false
34+
}
35+
36+
def foo5b(x: Option[Int | Double | String]) = x match { // warning: Some(_: String) not matched
37+
case Some(_: (Int | Double)) => true
38+
case None => false
39+
}
40+
}

0 commit comments

Comments
 (0)