@@ -3550,6 +3550,44 @@ static int Test10(S1 u)
35503550#line 1000
35513551 return u switch { null => 3, not int => 1 };
35523552 }
3553+
3554+ static int Test11(S1 u)
3555+ {
3556+ #line 1100
3557+ return u switch { not null => 1 };
3558+ }
3559+
3560+ static int Test11_5(S1 u)
3561+ {
3562+ #nullable enable
3563+ #line 1150
3564+ return u switch { not null => 1 };
3565+ #nullable disable
3566+ }
3567+
3568+ static int Test12(S1 u)
3569+ {
3570+ #line 1200
3571+ return u switch { null => 3, not null => 1 };
3572+ }
3573+
3574+ static int Test13(S1 u)
3575+ {
3576+ #line 1300
3577+ return u switch { not null => 3, null => 1 };
3578+ }
3579+
3580+ static int Test14(S1 u)
3581+ {
3582+ #line 1400
3583+ return u switch { { } => 1, null => 3 };
3584+ }
3585+
3586+ static int Test15(S1 u)
3587+ {
3588+ #line 1500
3589+ return u switch { null => 3, var x => 1 };
3590+ }
35533591}
35543592" ;
35553593 var comp = CreateCompilation ( [ src , IUnionSource ] ) ;
@@ -3580,7 +3618,20 @@ static int Test10(S1 u)
35803618 Diagnostic ( ErrorCode . WRN_SwitchExpressionNotExhaustive , "switch" ) . WithArguments ( "int" ) . WithLocation ( 900 , 18 ) ,
35813619 // (1000,18): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'int' is not covered.
35823620 // return u switch { null => 3, not int => 1 };
3583- Diagnostic ( ErrorCode . WRN_SwitchExpressionNotExhaustive , "switch" ) . WithArguments ( "int" ) . WithLocation ( 1000 , 18 )
3621+ Diagnostic ( ErrorCode . WRN_SwitchExpressionNotExhaustive , "switch" ) . WithArguments ( "int" ) . WithLocation ( 1000 , 18 ) ,
3622+ // (1150,18): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive). For example, the pattern 'null' is not covered.
3623+ // return u switch { not null => 1 };
3624+ Diagnostic ( ErrorCode . WRN_SwitchExpressionNotExhaustiveForNull , "switch" ) . WithArguments ( "null" ) . WithLocation ( 1150 , 18 ) ,
3625+ // (1200,42): hidden CS9335: The pattern is redundant.
3626+ // return u switch { null => 3, not null => 1 };
3627+ // Note the location, the diagnostic is for 'null' in 'not null' of the second case rather than for 'null' in the first case.
3628+ Diagnostic ( ErrorCode . HDN_RedundantPattern , "null" ) . WithLocation ( 1200 , 42 ) ,
3629+ // (1300,42): hidden CS9335: The pattern is redundant.
3630+ // return u switch { not null => 3, null => 1 };
3631+ Diagnostic ( ErrorCode . HDN_RedundantPattern , "null" ) . WithLocation ( 1300 , 42 ) ,
3632+ // (1400,37): hidden CS9335: The pattern is redundant.
3633+ // return u switch { { } => 1, null => 3 };
3634+ Diagnostic ( ErrorCode . HDN_RedundantPattern , "null" ) . WithLocation ( 1400 , 37 )
35843635 ) ;
35853636
35863637 verifier . VerifyIL ( "Program.Test1" , @"
@@ -4298,6 +4349,36 @@ static int Test8(S1 u)
42984349#line 800
42994350 return u switch { object => 1, null => 3 };
43004351 }
4352+
4353+ static int Test9(S1 u)
4354+ {
4355+ #line 900
4356+ return u switch { not null => 1 };
4357+ }
4358+
4359+ static int Test10(S1 u)
4360+ {
4361+ #line 1000
4362+ return u switch { null => 3, not null => 1 };
4363+ }
4364+
4365+ static int Test11(S1 u)
4366+ {
4367+ #line 1100
4368+ return u switch { not null => 3, null => 1 };
4369+ }
4370+
4371+ static int Test12(S1 u)
4372+ {
4373+ #line 1200
4374+ return u switch { { } => 1, null => 3 };
4375+ }
4376+
4377+ static int Test13(S1 u)
4378+ {
4379+ #line 1300
4380+ return u switch { null => 3, var x => 1 };
4381+ }
43014382}
43024383" ;
43034384 var comp = CreateCompilation ( [ src , IUnionSource ] ) ;
@@ -4325,7 +4406,19 @@ static int Test8(S1 u)
43254406 Diagnostic ( ErrorCode . ERR_PatternWrongType , "object" ) . WithArguments ( "S1" , "object" ) . WithLocation ( 700 , 43 ) ,
43264407 // (800,27): error CS8121: An expression of type 'S1' cannot be handled by a pattern of type 'object'.
43274408 // return u switch { object => 1, null => 3 };
4328- Diagnostic ( ErrorCode . ERR_PatternWrongType , "object" ) . WithArguments ( "S1" , "object" ) . WithLocation ( 800 , 27 )
4409+ Diagnostic ( ErrorCode . ERR_PatternWrongType , "object" ) . WithArguments ( "S1" , "object" ) . WithLocation ( 800 , 27 ) ,
4410+ // (900,18): warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive). For example, the pattern 'null' is not covered.
4411+ // return u switch { not null => 1 };
4412+ Diagnostic ( ErrorCode . WRN_SwitchExpressionNotExhaustiveForNull , "switch" ) . WithArguments ( "null" ) . WithLocation ( 900 , 18 ) ,
4413+ // (1000,42): hidden CS9335: The pattern is redundant.
4414+ // return u switch { null => 3, not null => 1 };
4415+ Diagnostic ( ErrorCode . HDN_RedundantPattern , "null" ) . WithLocation ( 1000 , 42 ) ,
4416+ // (1100,42): hidden CS9335: The pattern is redundant.
4417+ // return u switch { not null => 3, null => 1 };
4418+ Diagnostic ( ErrorCode . HDN_RedundantPattern , "null" ) . WithLocation ( 1100 , 42 ) ,
4419+ // (1200,37): hidden CS9335: The pattern is redundant.
4420+ // return u switch { { } => 1, null => 3 };
4421+ Diagnostic ( ErrorCode . HDN_RedundantPattern , "null" ) . WithLocation ( 1200 , 37 )
43294422 ) ;
43304423 }
43314424 }
0 commit comments