Skip to content

CA2012 false positive when consuming ValueTask through a C# 14 extension block #56872

Open

Description

@aradalvand

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:

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 = warning

Expected 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

  1. added
    untriaged
    on Oct 31, 2026
  2. locked and limited conversation to collaborators on Oct 31, 2026
  3. unlocked this conversation on Oct 31, 2026
  4. removed
    untriaged
    on Oct 31, 2026
  5. github-actions commented on Oct 31, 2026

    @github-actions
    Contributor

    🎯 Agentic Issue Triage

    Triage Assessment:

    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.

    Generated by 🏷️ Issue Triage for #56872 · copilot · auto · 21.5 AIC · ⌖ 0.786 AIC · ⊞ 14.4K · ◷

  6. github-actions commented on Nov 7, 2026

    @github-actions
    Contributor

    🍪 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! 🍪

    🍪 Om nom nom by Issue Monster Orchestrator · copilot · auto · 14.3 AIC · ⌖ 15.8 AIC · ⊞ 15.2K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions