Repository navigation
Description
Describe the bug
CA2012 reports a false positive when a returned ValueTask<T> is immediately consumed by an extension method declared using a C# 14 extension block.
The extension method awaits its receiver exactly once, and the caller immediately awaits the extension method's result. There is no stored or multiply consumed ValueTask. The equivalent traditional this ValueTask<T> extension method does not produce the warning.
This was already reported in the former analyzer repository:
- False positive CA2012 with extension block roslyn-analyzers#7784
- CA2012 yields a false positive when using C# 14's extension block feature roslyn-analyzers#7789
Both reports remain open. Filing here because the repository consolidation notice directs Microsoft.CodeAnalysis.NetAnalyzers development and issues to dotnet/sdk.
To reproduce
The following minimal reproduction is adapted from the existing reports. Use a console project targeting net10.0 with C# 14 or later and CA2012 enabled.
using System.Threading.Tasks;
await GetValueAsync().ViaExtensionBlock(); // CA2012 on GetValueAsync()
await GetValueAsync().ViaTraditionalExtension(); // No CA2012
await Extensions.ViaExtensionBlock(GetValueAsync()); // No CA2012
static ValueTask<int> GetValueAsync() => new(42);
public static class Extensions
{
extension(ValueTask<int> task)
{
public async ValueTask<int> ViaExtensionBlock() => await task;
}
public static async ValueTask<int> ViaTraditionalExtension(
this ValueTask<int> task) => await task;
}If needed, enable the diagnostic explicitly in .editorconfig:
[*.cs]
dotnet_diagnostic.CA2012.severity = warningExpected behavior
All three calls should be accepted. Each passes the returned ValueTask<int> directly to a method that awaits it exactly once.
Actual behavior
The extension-block invocation produces CA2012:
ValueTask instances returned from method calls should be directly awaited, returned, or passed as an argument to another method call.
The traditional extension-method and explicit static-call forms avoid the warning, as documented in the linked reports.
In our application, this occurs throughout chains such as await authorizer.Ensure(request).Try(errorMapper). Ensure returns ValueTask<Result<T, TError>>, and Try is declared in an extension block with the implementation (await task).Try(errorMapper).
Environment
Current affected application:
- .NET SDK:
11.0.100-rc.3.26522.148 - Target framework:
net10.0 - Language version:
preview - OS: Arch Linux, x64
The original report dotnet/roslyn-analyzers#7784 reproduced the issue with .NET SDK 10.0.101. The minimal reproduction above is adapted from the existing reports; it has not been separately built with the current application's SDK.
Activity
github-actions commentedon Oct 31, 2026
🎯 Agentic Issue Triage
Triage Assessment:
untriagedremoved)@Evangelink|@dotnet/dotnet-analyzershigh- CA2012 is a NetAnalyzers rule and the CODEOWNERS section is unambiguous.Similar issues: #54340 (
potentially related)➡️ Summary: CA2012 falsely flags a ValueTask consumed by a C# 14 extension-block method, though the traditional extension form is not flagged. Verify how the CA2012 analyzer handles extension-block receivers.
github-actions commentedon Nov 7, 2026
🍪 Issue Monster selected this for Copilot
I've identified this issue as a good candidate for automated resolution and requested assignment to the Copilot coding agent.
If assignment succeeds, the Copilot coding agent will analyze the issue and create a pull request with the fix.
Om nom nom! 🍪