Skip to content

Nullable reference types: no nullability flow check in switch statements. #23944

@orthoxerox

Description

@orthoxerox

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;
        }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions