Skip to content

IDE0305 "collection initialization can be simplified" spoils chained LINQ calls #70833

@Timovzl

Description

@Timovzl

(I couldn't find a more appropriate type than "bug report", but feel free to adjust this is there is a mroe correct option.)

Version Used:

.NET 8, VS 17.8.0.

Steps to Reproduce:

private static class EnumValueCache<T>
    where T : struct, Enum
{
    public static readonly ImmutableArray<T> SortedValues = Enum.GetValues<T>()
        .Order()
        .ToImmutableArray();
}

The analyzer suggests the following:

private static class EnumValueCache<T>
   where T : struct, Enum
{
   public static readonly ImmutableArray<T> SortedValues =
   [
      .. Enum.GetValues<T>()
               .Order()
,
   ];
}

Diagnostic Id:

IDE0305.

Expected Behavior:

The result should be a readability improvement.

I'm not sure if this is the perfect analysis, but here's a quick attempt: A LINQ To...() call chained after other LINQ extension calls should not be flagged by the analyzer, since that would promote reduced readability.

Actual Behavior:

The analyzer suggests using a collection expression, thus significantly reducing the readability of the code.

Although I'm aware that the analyzer can be suppressed, its behavior is very desirable for non-chained callsL

new[] { 1, 2, 3, }.ToImmutableArray() => [ 1, 2, 3, ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions