Repository navigation
Description
Describe the bug
When a type implements the LINQ query pattern with methods that return ValueTask<T>, CA2012 reports the outermost query operator call. This happens even when the query's result is returned directly or awaited directly. The same code in method syntax doesn't warn.
The cause seems to be that, in the IOperation tree, the outermost Select/SelectMany invocation sits under an ITranslatedQueryOperation. The analyser doesn't look through that node, so it never sees the parent IReturnOperation / IAwaitOperation and treats the result as consumed some other way.
This is separate from #56983 (receivers of C# 14 extension block members). The repro below only uses classic extension methods.
Steps to reproduce
SDK 11.0.100, net10.0, <AnalysisLevel>latest-Minimum</AnalysisLevel>
public static class ValueTaskQueryExtensions
{
public static async ValueTask<TResult> Select<T, TResult>(
this ValueTask<T> source,
Func<T, TResult> selector)
=> selector(await source);
public static async ValueTask<TResult> SelectMany<T, TMiddle, TResult>(
this ValueTask<T> source,
Func<T, ValueTask<TMiddle>> collectionSelector,
Func<T, TMiddle, TResult> resultSelector)
{
T value = await source;
return resultSelector(value, await collectionSelector(value));
}
}
public static class Repro
{
static ValueTask<int> GetAsync() => ValueTask.FromResult(42);
// No warning:
public static ValueTask<int> MethodSyntax()
=> GetAsync().SelectMany(a => GetAsync(), (a, b) => a + b);
// CA2012:
public static ValueTask<int> QueryReturned()
=> from a in GetAsync()
from b in GetAsync()
select a + b;
// CA2012:
public static async Task<int> QueryAwaited()
=> await (from a in GetAsync()
from b in GetAsync()
select a + b);
// CA2012:
public static ValueTask<int> QuerySelectOnly()
=> from a in GetAsync()
select a * 2;
}
Expected behavior
No CA2012. The ValueTask produced by the query is returned or awaited directly, the same as in MethodSyntax.
Actual behavior
CA2012 is reported on the last query clause (the from b ... clause, or the select clause in QuerySelectOnly) for all three query forms.
Is this a regression?
Unknown.
Are there any workarounds?
Suppress the warning, or rewrite everything to use method syntax.
dotnet --info output
.NET SDK:
Version: 11.0.100
Commit: 297acf930f
Workload version: 11.0.100-manifests.5d612b92
MSBuild version: 18.12.2-1.26529.118+297acf930
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26300
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\11.0.100
.NET workloads installed:
[maui-windows]
Installation Source: VS 18.12.12405.243
Manifest Version: 11.0.0/11.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\11.0.100\microsoft.net.sdk.maui\11.0.0\WorkloadManifest.json
Install Type: Msi
[maccatalyst]
Installation Source: VS 18.12.12405.243
Manifest Version: 27.2.11039/11.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\11.0.100\microsoft.net.sdk.maccatalyst\27.2.11039\WorkloadManifest.json
Install Type: Msi
[ios]
Installation Source: VS 18.12.12405.243
Manifest Version: 27.2.11039/11.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\11.0.100\microsoft.net.sdk.ios\27.2.11039\WorkloadManifest.json
Install Type: Msi
[android]
Installation Source: VS 18.12.12405.243
Manifest Version: 37.2.29/11.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\11.0.100\microsoft.net.sdk.android\37.2.29\WorkloadManifest.json
Install Type: Msi
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.
Host:
Version: 11.0.0
Architecture: x64
Commit: 297acf930f
.NET SDKs installed:
11.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.34 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 11.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.34 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 11.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.34 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.23 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 11.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not foundIDE version
Visual Studio 2026 v18.12; Visual Studio Code v1.146
Other details
Suggested fix: when walking up from the invocation, treat an ITranslatedQueryOperation parent as transparent, the same way a conversion or parenthesised expression would be.
Activity
RichardD2 commentedon Nov 13, 2026
Simple reproduction project
github-actions commentedon Nov 13, 2026
🎯 Agentic Issue Triage
Triage Assessment:
@krwq| @dotnet/dotnet-analyzershigh- CA2012 is an analyzer in NetAnalyzers, and the report names it directly.Similar issues: #56872 (
potentially related)➡️ Summary: CA2012 falsely warns when a ValueTask-returning LINQ query expression is returned or awaited, because ITranslatedQueryOperation is not looked through. Analyzer owners should confirm and treat that node as transparent.