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

[Proposal]: Nullable analysis of LINQ queries #3951

Open
1 of 5 tasks
jcouv opened this issue Sep 28, 2020 · 2 comments
Open
1 of 5 tasks

[Proposal]: Nullable analysis of LINQ queries #3951

jcouv opened this issue Sep 28, 2020 · 2 comments

Comments

@jcouv
Copy link
Member

jcouv commented Sep 28, 2020

Nullable analysis of LINQ queries

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
  • Specification: Not Started

Summary

Nullable analysis of LINQ queries (LDM expressed interested to handle Where, needs design proposal)

Motivation

Range variables in LINQ queries are currently treated as oblivious to minimize annoyances.
Most of the annoyance results from our inability to recognize method calls that filter out null elements, such as .Where(i => i is not null).

using System.Linq;
using System.Collections.Generic;

#nullable enable
public class C 
{
    public void M(IEnumerable<string?> list) 
    {
        _ = from item in list
            select item.ToString(); // should warn

        _ = list
            .Where(i => i is not null)
            .Select(i => i.ToString()); // should not warn
    }
}

Detailed design

TBD

Unresolved questions

  • How to recognize Where methods?

Design meetings

Split issue from #3868
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-28.md#nullability-improvements
https://github.com/dotnet/csharplang/blob/main/meetings/2024/LDM-2024-09-06.md#nullable-analysis-of-linq-queries

@gafter
Copy link
Member

gafter commented Dec 16, 2020

Would these warnings only be suppressed for the "standard" Linq implementations that have the expected semantics, or for all query expressions (even those that do not have the expected semantics)?

@JTeeuwissen
Copy link

Note that OfType currently can be used to filter out nulls whilst getting the correct return type. But perhaps there are more cases where this would not apply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants