File tree 4 files changed +52
-5
lines changed
src/dotty/tools/dotc/transform/patmat
tests/explicit-nulls/warn
4 files changed +52
-5
lines changed Original file line number Diff line number Diff line change @@ -903,7 +903,7 @@ object SpaceEngine {
903
903
then project(OrType (selTyp, ConstantType (Constant (null )), soft = false ))
904
904
else project(selTyp)
905
905
)
906
-
906
+
907
907
var i = 0
908
908
val len = cases.length
909
909
var prevs = List .empty[Space ]
@@ -925,11 +925,17 @@ object SpaceEngine {
925
925
report.warning(MatchCaseUnreachable (), pat.srcPos)
926
926
if pat != EmptyTree // rethrow case of catch uses EmptyTree
927
927
&& ! pat.symbol.isAllOf(SyntheticCase , butNot= Method ) // ExpandSAMs default cases use SyntheticCase
928
- && isSubspace(covered, prev)
928
+ && isSubspace(covered, Or ( List ( prev, Typ (defn. NullType )))) // for when Null is not subtype of AnyRef under explicit nulls
929
929
then {
930
- val nullOnly = isNullable && i == len - 1 && isWildcardArg(pat)
931
- val msg = if nullOnly then MatchCaseOnlyNullWarning () else MatchCaseUnreachable ()
932
- report.warning(msg, pat.srcPos)
930
+ val nullOnly =
931
+ (isNullable || (defn.NullType <:< selTyp))
932
+ && i == len - 1
933
+ && isWildcardArg(pat)
934
+ if nullOnly then {
935
+ report.warning(MatchCaseOnlyNullWarning (), pat.srcPos)
936
+ } else if isSubspace(covered, prev) then {
937
+ report.warning(MatchCaseUnreachable (), pat.srcPos)
938
+ }
933
939
}
934
940
deferred = Nil
935
941
}
Original file line number Diff line number Diff line change @@ -212,6 +212,11 @@ class CompilationTests {
212
212
)
213
213
}.checkCompile()
214
214
215
+ @ Test def explicitNullsWarn : Unit = {
216
+ implicit val testGroup : TestGroup = TestGroup (" explicitNullsWarn" )
217
+ compileFilesInDir(" tests/explicit-nulls/warn" , explicitNullsOptions)
218
+ }.checkWarnings()
219
+
215
220
@ Test def explicitNullsRun : Unit = {
216
221
implicit val testGroup : TestGroup = TestGroup (" explicitNullsRun" )
217
222
compileFilesInDir(" tests/explicit-nulls/run" , explicitNullsOptions)
Original file line number Diff line number Diff line change
1
+ -- [E121] Pattern Match Warning: tests/explicit-nulls/warn/i21577.scala:5:9 --------------------------------------------
2
+ 5 | case _ => println(2) // warn
3
+ | ^
4
+ | Unreachable case except for null (if this is intentional, consider writing case null => instead).
5
+ -- [E121] Pattern Match Warning: tests/explicit-nulls/warn/i21577.scala:12:11 ------------------------------------------
6
+ 12 | case _ => println(2) // warn
7
+ | ^
8
+ | Unreachable case except for null (if this is intentional, consider writing case null => instead).
9
+ -- [E121] Pattern Match Warning: tests/explicit-nulls/warn/i21577.scala:18:11 ------------------------------------------
10
+ 18 | case _ => println(2) // warn
11
+ | ^
12
+ | Unreachable case except for null (if this is intentional, consider writing case null => instead).
Original file line number Diff line number Diff line change
1
+ def f (s : String ) =
2
+ val s2 = s.trim()
3
+ s2 match
4
+ case s3 : String => println(1 )
5
+ case _ => println(2 ) // warn
6
+
7
+
8
+ def f2 (s : String | Null ) =
9
+ val s2 = s.nn.trim()
10
+ s2 match
11
+ case s3 : String => println(1 )
12
+ case _ => println(2 ) // warn
13
+
14
+ def f3 (s : String | Null ) =
15
+ val s2 = s
16
+ s2 match
17
+ case s3 : String => println(1 )
18
+ case _ => println(2 ) // warn
19
+
20
+ def f4 (s : String | Int ) =
21
+ val s2 = s
22
+ s2 match
23
+ case s3 : String => println(1 )
24
+ case _ => println(2 )
You can’t perform that action at this time.
0 commit comments