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

Should not warn dereferencing collection after foreach #30562

Closed
cston opened this issue Oct 17, 2018 · 2 comments
Closed

Should not warn dereferencing collection after foreach #30562

cston opened this issue Oct 17, 2018 · 2 comments
Assignees
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. New Language Feature - Nullable Reference Types Nullable Reference Types Test Test failures in roslyn-CI
Milestone

Comments

@cston
Copy link
Member

cston commented Oct 17, 2018

The second foreach in each case should not produce a warning since the collection cannot be null after the first dereference.

[module: System.Runtime.CompilerServices.NonNullTypes]
class Enumerable
{
    public System.Collections.IEnumerator GetEnumerator() => throw null;
}
class Program
{
    static void F1(object[]? x)
    {
        foreach (var i in x) { } // warning: possibly null
        foreach (var i in x) { } // ok
    }
    static void F2(Enumerable? y)
    {
        foreach (var i in y) { } // warning: possibly null
        foreach (var i in y) { } // ok
    }
}
@cston
Copy link
Member Author

cston commented Oct 17, 2018

See NullableReferenceTypesTests.NotNullAfterDereference_ForEach.

@jaredpar jaredpar added this to the 16.0 milestone Oct 24, 2018
@jaredpar jaredpar added the Bug label Oct 29, 2018
@jcouv jcouv added the Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. label Nov 10, 2018
@jcouv jcouv self-assigned this Jan 5, 2019
@jcouv jcouv modified the milestones: 16.0, 16.0.P2 Jan 5, 2019
@jcouv
Copy link
Member

jcouv commented Jan 5, 2019

It looks like I incorrectly adjusted NotNullAfterDereference_ForEach, so it's no longer verifying what we want. I'll rectify the test in my next PR.

@jcouv jcouv added Test Test failures in roslyn-CI and removed Bug labels Jan 5, 2019
@jcouv jcouv modified the milestones: 16.0.P2, 16.0.P3 Jan 11, 2019
@jcouv jcouv added 3 - Working 4 - In Review A fix for the issue is submitted for review. and removed 3 - Working labels Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - In Review A fix for the issue is submitted for review. Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. New Language Feature - Nullable Reference Types Nullable Reference Types Test Test failures in roslyn-CI
Projects
None yet
Development

No branches or pull requests

3 participants