dotnet / dotnet/sdk

CA2012 false positive, when calling `ConfigureAwait` on result of ternary operator.

Open
#54,340 0 comments 0 reactions 0 assignees View on GitHub
Area-Microsoft.CodeAnalysis.NetAnalyzers untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Describe the bug
Calling `ConfigureAwait` on result of ternary operator, does not recognized as valid `ValueTask` usage, which cause CA2012 diagnostic. Adding additional operation between ternary operator and `ConfigureAwait` call, silence diagnostic.

### To Reproduce
Test.csproj:
```xml


net10.0
enable
Latest
All

```
Test.cs:
```csharp
using System.Threading.Tasks;

namespace Test;

static class Test
{
static ValueTask M(this ValueTask vt) => vt;
static ValueTask M1() => ValueTask.CompletedTask;
static ValueTask M2() => ValueTask.CompletedTask;
static async ValueTask T1(bool b) => await (b ? M1() : M2()).ConfigureAwait(false);
static async ValueTask T2(bool b) => await (b ? M1() : M2()).M().ConfigureAwait(false);
}
```

### Exceptions (if any)
```
Restore complete (0,6s)
Test net10.0 succeeded with 2 warning(s) (0,5s) → bin\Debug\net10.0\Test.dll
M:\Temp\Test\Test.cs(10,50): warning CA2012: ValueTask instances returned from method calls should be directly awaited, returned, or passed as an argument to another method call. Other usage, such as storing an instance into a local or a field, is likely an indication of a bug, as ValueTask instances must only ever be consumed once. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2012)
M:\Temp\Test\Test.cs(10,57): warning CA2012: ValueTask instances returned from method calls should be directly awaited, returned, or passed as an argument to another method call. Other usage, such as storing an instance into a local or a field, is likely an indication of a bug, as ValueTask instances must only ever be consumed once. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2012)

Build succeeded with 2 warning(s) in 1,9s
```
Warnings on `M1()` and `M2()` in `T1` method. No warnings produced for `T2` method.

### Further technical details
details of dotnet --info

```
.NET SDK:
Version: 10.0.300
Commit: caa81fa497
Workload version: 10.0.300-manifests.b47d7e23
MSBuild version: 18.6.3+caa81fa49

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.300\

.NET workloads installed:
There are no installed workloads to display.
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: 10.0.8
Architecture: x64
Commit: 94ea82652c

.NET SDKs installed:
10.0.300 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 10.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 10.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 10.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
None

Environment variables:
Not set

global.json file:
Not found
```

VS Code

```
Version: 1.120.0 (system setup)
Commit: 0958016b2af9f09bb4257e0df4a95e2f90590f9f
Date: 2026-05-12T20:17:22Z
Electron: 39.8.8
ElectronBuildId: 13870025
Chromium: 142.0.7444.265
Node.js: 22.22.1
V8: 14.2.231.22-electron.0
OS: Windows_NT x64 10.0.26200
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.