Skip to content

Code fix for IDE0028 produces invalid collection expression #71240

@meziantou

Description

@meziantou

Version Used: '4.9.0-2.23578.7 (984df16)'. Language version: 12.0

Steps to Reproduce:

The actual use case is using Xunit.TheoryData<T>, but here's a small repro

public class UnitTest1
{
    void A()
    {
        Sample<string> a = new Sample<string> // IDE0028: Collection expression can be simplified
        {
            "test",
        };

        // After applying the fix
        Sample<string> a =
        [
            "test", // CS0029 Cannot implicitly convert type 'string' to 'object[]' 
        ];
    }
}

class Sample<T> : IEnumerable<object[]>
{
    public void Add(T p) => throw null;
    IEnumerator IEnumerable.GetEnumerator() => throw null;
    IEnumerator<object[]> IEnumerable<object[]>.GetEnumerator() => throw null;
}

Diagnostic Id: IDE0028

Expected Behavior: The code is valid after applying the code fix

Actual Behavior: The build fails

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions