Analyzer rule

Spec: LC043 - Async Enumerable Buffering

EF Core LINQ performance analyzer and Roslyn analyzer for catching query issues at compile time.

Spec: LC043 - Async Enumerable Buffering

Goal

Detect immediate buffering of an IAsyncEnumerable<T> into a list or array before a single foreach.

The Problem

Buffering an async stream into memory and then looping exactly once throws away streaming behavior for no benefit.

Example Violation

var users = await stream.ToListAsync();
foreach (var user in users)
{
    Console.WriteLine(user.Name);
}

The Fix

Stream directly with await foreach.

await foreach (var user in stream)
{
    Console.WriteLine(user.Name);
}

Analyzer Logic

ID: LC043

Category: Performance

Severity: Info

Notes

This v1 rule is intentionally narrow. It reports only immediate buffer-then-loop patterns that are safe to rewrite to await foreach. The buffered call must come from a proven IAsyncEnumerable<T> source, so custom non-stream helpers named ToListAsync or ToArrayAsync stay quiet. Calls with buffer-method arguments, such as cancellation tokens, are left alone so the fixer does not drop behavior. Buffers captured by nested lambdas or local functions are also left alone because removing the local would break that captured use.