Skip to content

Should not warn dereferencing collection after foreach #30562

@cston

Description

@cston

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
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions