-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Closed
Labels
Area-CompilersBugFeature - Nullable Reference TypesNullable Reference TypesNullable Reference TypesResolution-FixedThe bug has been fixed and/or the requested behavior has been implementedThe bug has been fixed and/or the requested behavior has been implemented
Milestone
Description
Version Used: features/NullableReferenceTypes on sharplab.io
Steps to Reproduce:
using System;
public class C {
static string? Get()
{
return new Random().NextDouble() > 0.5 ? "" : null;
}
public void M() {
string? s = Get();
switch(s) {
case null:
Console.WriteLine("null");
break;
default:
Console.WriteLine(s.Length);
break;
}
}
}Expected Behavior:
No warnings reported.
Actual Behavior:
Warning CS8602: "Possible dereference of a null reference." is reported for the default case. I expected the compiler to infer the non-nullability of s like it does in
if (s == null) {
Console.WriteLine("null");
} else {
Console.WriteLine(s.Length);
}and nns in
switch(s) {
case null:
Console.WriteLine("null");
break;
case var nns:
Console.WriteLine(nns.Length);
break;
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Area-CompilersBugFeature - Nullable Reference TypesNullable Reference TypesNullable Reference TypesResolution-FixedThe bug has been fixed and/or the requested behavior has been implementedThe bug has been fixed and/or the requested behavior has been implemented