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

C#8 compiler reports CS8620 for IEnumerable<T?> after filtering for not null #38812

Closed
Bonuspunkt opened this issue Sep 24, 2019 · 3 comments
Closed
Labels
Area-Compilers Area-Language Design New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Milestone

Comments

@Bonuspunkt
Copy link

Version Used:
dotnet-sdk-3.0.100

Steps to Reproduce:
enable nullable

public static void Run()
{
    var enumerable = Enumerable.Range(1, 10).Select(i => i % 2 == 0 ? null : i.ToString());
    var notNulls = enumerable.Where(item => item != null);
    Check(notNulls);
}
private static void Check(IEnumerable<string> notNull) { }

Expected Behavior:
no warning

Actual Behavior:
Warning CS8620 Argument of type 'IEnumerable<string?>' cannot be used for parameter 'notNull' of type 'IEnumerable' in 'void Check(IEnumerable notNull)' due to differences in the nullability of reference types.

my current workaround is

    var notNulls = enumerable.Where(item => item != null).Select(item => item!);
@huoyaoyuan
Copy link
Member

Where(x => x != null) is quite hard to deal for nullable analysis.
Potential solutions:

  • A concrete method IEnumerable<T> WhereNotNull<T>(IEnumerable<T?>), using yield return itself.
  • Use OfType<T>. The semantic of OfType equals to null-check when applied to the same reference type.

@RikkiGibson
Copy link
Contributor

RikkiGibson commented Sep 24, 2019

Perhaps a duplicate of dotnet/csharplang#8383

@jcouv
Copy link
Member

jcouv commented Dec 13, 2019

Yes, this is a duplicate of dotnet/csharplang#8383
I'll go ahead and close as such. Thanks

@jcouv jcouv closed this as completed Dec 13, 2019
@jcouv jcouv added the Resolution-Duplicate The described behavior is tracked in another issue label Dec 13, 2019
@sharwell sharwell closed this as not planned Won't fix, can't repro, duplicate, stale Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Area-Language Design New Language Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

5 participants