Skip to main content

RedundantSpreadElement

Diagnostic Rule Overview

FieldValue
IDSHIMMER1020
Analyzer titleInline spread element
Analyzer messageInline spread element
Code fix titleFlatten spread element
Default severityWarning
Minimum framework/language versionC# 12
CategoryShimmeringUsage
Link to codeRedundantSpreadElementAnalyzer.cs
Code fix exists?Yes

Detailed Explanation

Explicitly creating an array before spreading it in a collection expression is redundant and should be inlined.

Examples

Flagged code:

namespace Tests;
class Test
{
int[] Array => [1, .. new[] { 2, 3 }, 4];
}

Fixed code:

namespace Tests;
class Test
{
int[] Array => [1, 2, 3, 4];
}

Justification of the Severity

While this is not a bug, this will slow down your code and increase memory usage.