Skip to content

CA2012 false positive when a ValueTask-returning query expression is returned or awaited #57101

Open

Description

@RichardD2

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 found

IDE 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

  1. added
    untriaged
    on Nov 13, 2026
  2. RichardD2 commented on Nov 13, 2026

    @RichardD2
    Author
  3. locked and limited conversation to collaborators on Nov 13, 2026
  4. unlocked this conversation on Nov 13, 2026
  5. added and removed
    untriaged
    on Nov 13, 2026
  6. github-actions commented on Nov 13, 2026

    @github-actions
    Contributor

    🎯 Agentic Issue Triage

    Triage Assessment:

    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.

    Generated by 🏷️ Issue Triage for #57101 · copilot · auto · ~17 AIC · ⌖ ~1 AIC · ⊞ 14.6K · ◷

  7. removed their assignment
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions