Skip to content

Roslyn fails to verify nullable reference types' safety in collection expressions #72142

@TessenR

Description

@TessenR

Version Used:

Microsoft Visual Studio Professional 2022
Version 17.10.0 Preview 1.0
VisualStudio.17.Preview/17.10.0-pre.1.0+34607.79
Microsoft .NET Framework
Version 4.8.09037

Steps to Reproduce:

Compile and run the following code:

using System;
using System.Collections.Generic;
#nullable enable

List<string> list = [null];
Console.WriteLine(list[0].Length);

sharplab.io

Expected Behavior:
There should be a warning about mismatching nullability somewhere on the line List<string> list = [null];

Actual Behavior:
No warnings at all.
The program crashes at runtime with a NullReferenceException

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions