Skip to content

Commit 6dad94d

Browse files
committed
don't warn for null
1 parent ceb0596 commit 6dad94d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,11 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
921921
else Empty
922922
}.reduce((a, b) => Or(List(a, b)))
923923

924+
def isNull(tree: Tree): Boolean = tree match {
925+
case Literal(Constant(null)) => true
926+
case _ => false
927+
}
928+
924929
(1 until cases.length).foreach { i =>
925930
val prevs = projectPrevCases(cases.take(i))
926931

@@ -944,7 +949,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
944949
}
945950

946951
// if last case is `_` and only matches `null`, produce a warning
947-
if (i == cases.length - 1) {
952+
if (i == cases.length - 1 && !isNull(pat) ) {
948953
simplify(minus(covered, prevs)) match {
949954
case Typ(ConstantType(Constant(null)), _) =>
950955
ctx.warning(MatchCaseOnlyNullWarning(), pat.pos)

tests/patmat/i4225c.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object Bar {
2+
def unapply(x: String): Some[Int] =
3+
Some(0)
4+
}
5+
6+
object Test {
7+
def test(x: String) =
8+
x match {
9+
case Bar(a) => a
10+
case null => x
11+
}
12+
}

0 commit comments

Comments
 (0)