Skip to content

False positive CS9223 in collection expression arguments #82562

@Spolutrean

Description

@Spolutrean

Steps to Reproduce:
Consider this code .NET lab:

class MyCollection<T> : IEnumerable<T>
{
    private readonly List<T> _list;
    IEnumerator<T> IEnumerable<T>.GetEnumerator() => _list.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator();
    public MyCollection() { _list = new(); }
    public MyCollection(params MyCollection<T> other) { _list = new(other); }
    public void Add(T t) { _list.Add(t); }
}

class C
{
    public void M()
    {
        MyCollection<int> c = [with(1)]; // Creation of params collection 'MyCollection<int>' results in an infinite chain of invocation of constructor 'MyCollection<T>.MyCollection()'.
    }
}

Diagnostic Id: CS9223

Expected Behavior:
No CS9223 is reported, because the call stack will be MyCollection(params MyCollection<T> other) --> MyCollection() --> .Add() and no infinite recursion is happened. This exact behavior is fixed under a test CollectionExpressionTests_WithElement_Extra.ParamsCycle_MultipleConstructors

Actual Behavior:
CS9223 is reported

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions