Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing nullability warning for spreads of maybe-null collections #74667

Closed
jnm2 opened this issue Aug 6, 2024 · 1 comment · Fixed by #74686
Closed

Missing nullability warning for spreads of maybe-null collections #74667

jnm2 opened this issue Aug 6, 2024 · 1 comment · Fixed by #74686
Labels
Area-Compilers Feature - Collection Expressions Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@jnm2
Copy link
Contributor

jnm2 commented Aug 6, 2024

Version Used: 17.11 preview 6

There should be a warning "CS8602 Dereference of a possibly null reference" in the following example, but there is not:

List<int>? maybeNull = null;
List<int> clone = [.. maybeNull];

When this code runs, it throws NullReferenceException.

In both https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/collection-expressions#known-length-translation and https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/collection-expressions#unknown-length-translation, the translation uses foreach over the expression being spread, which would equivalently throw:

foreach (var _ in maybeNull) // Both warns and throws
@jaredpar
Copy link
Member

jaredpar commented Aug 8, 2024

This is effectively a dupe of #68786

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Collection Expressions Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants