File tree 6 files changed +49
-1
lines changed
tests/explicit-nulls/warn
6 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -215,6 +215,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
215
215
case TailrecNestedCallID // errorNumber: 199
216
216
case FinalLocalDefID // errorNumber: 200
217
217
case NonNamedArgumentInJavaAnnotationID // errorNumber: 201
218
+ case MatchCaseNonNullableWildcardWarningID // errorNumber: 202
218
219
219
220
def errorNumber = ordinal - 1
220
221
Original file line number Diff line number Diff line change @@ -935,6 +935,12 @@ extends PatternMatchMsg(MatchCaseOnlyNullWarningID) {
935
935
def explain (using Context ) = " "
936
936
}
937
937
938
+ class MatchCaseNonNullableWildcardWarning ()(using Context )
939
+ extends PatternMatchMsg (MatchCaseNonNullableWildcardWarningID ) {
940
+ def msg (using Context ) = i """ Unreachable case (if expression is expected to be nullable, consider giving it a nullable type annotation). """
941
+ def explain (using Context ) = " "
942
+ }
943
+
938
944
class MatchableWarning (tp : Type , pattern : Boolean )(using Context )
939
945
extends TypeMsg (MatchableWarningID ) {
940
946
def msg (using Context ) =
Original file line number Diff line number Diff line change @@ -928,7 +928,11 @@ object SpaceEngine {
928
928
&& isSubspace(covered, prev)
929
929
then {
930
930
val nullOnly = isNullable && i == len - 1 && isWildcardArg(pat)
931
- val msg = if nullOnly then MatchCaseOnlyNullWarning () else MatchCaseUnreachable ()
931
+ val wildcardNotNullable = i == len - 1 && isWildcardArg(pat)
932
+ val msg =
933
+ if nullOnly then MatchCaseOnlyNullWarning ()
934
+ else if wildcardNotNullable then MatchCaseNonNullableWildcardWarning ()
935
+ else MatchCaseUnreachable ()
932
936
report.warning(msg, pat.srcPos)
933
937
}
934
938
deferred = Nil
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
+ -- [E202] Pattern Match Warning: tests/explicit-nulls/warn/i21577.scala:5:9 --------------------------------------------
2
+ 5 | case _ => println(2) // warn
3
+ | ^
4
+ | Unreachable case (if expression is expected to be nullable, consider giving it a nullable type annotation).
5
+ -- [E202] Pattern Match Warning: tests/explicit-nulls/warn/i21577.scala:12:11 ------------------------------------------
6
+ 12 | case _ => println(2) // warn
7
+ | ^
8
+ | Unreachable case (if expression is expected to be nullable, consider giving it a nullable type annotation).
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 )
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