λ1013: Sequence.Return can be replaced with a collection expression
A single-element Sequence.Return(x) in a position that supplies a collection target type is the collection expression
[x].
Cause
Sequence.Return is called with one element where the compiler knows the target type: an argument, a return, an
expression body, a cast, an assignment or an explicitly typed initializer whose type is IEnumerable<T>,
IReadOnlyCollection<T> or IReadOnlyList<T>.
The rule only fires when the rewrite changes nothing but the spelling:
- the file is compiled as C# 12 or later, which collection expressions need;
- the call is the single-element overload, not the
paramsone.Sequence.Return(array)through theparamsoverload would become[array], a one-element list of arrays; - the collection expression has the same element type and binds to the same member, so an overload that only a collection expression would pick is left alone;
- the call is not inside an expression tree.
var x = Sequence.Return(1) is not reported, because var x = [1] does not compile, and neither is Sequence.Return
as the receiver of a LINQ chain: there is no target type to speak of, and Sequence.Return stays the right tool.
Reason for rule
A collection expression is the language's own spelling for a literal collection; it needs no helper and reads like the value it is. The rule is a hint rather than a style violation, so it reports an info.
How to fix violations
Replace the call with the collection expression. The code fix does this and keeps comments inside the call; a range without a left operand is parenthesized so that it does not turn into a spread.
Examples
Disallowed
static IEnumerable<int> Example()
=> Sequence.Return(42);
static void Example()
=> Consume(Sequence.Return("tag"));
static void Consume(IReadOnlyList<string> tags) { }
Allowed
static IEnumerable<int> Example()
=> [42];
static void Example()
=> Consume(["tag"]);
// No target type: the receiver of a LINQ chain.
static IEnumerable<int> Example()
=> Sequence.Return(42).Concat(More());
// The element type differs from the target, so this is not the same collection.
static IEnumerable<object> Example()
=> Sequence.Return("tag");