Skip to content

[WIP] Fix CA2012 false positive for ValueTask in C# 14 extension - #56983

Draft
nagilson with Copilot wants to merge 1 commit into
mainfrom
copilot/fix-ca2012-false-positive
Draft

nagilson with Copilot wants to merge 1 commit into
mainfrom
copilot/fix-ca2012-false-positive

Conversation

Copilot AI commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.


This section details on the original issue you should resolve

<issue_title>CA2012 false positive when consuming ValueTask through a C# 14 extension block</issue_title>
<issue_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:

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.
</issue_description>

Comments on the Issue (you are @copilot in this section)

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
3 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants