Open
Description
openedon Jan 25, 2024
Analyzer
Diagnostic ID: CA2012: Use ValueTasks correctly
Analyzer source
SDK: Built-in CA analyzers in .NET 5 SDK or later
Version: SDK 8.0.101
Describe the bug
The following generates CA2012
if (x?.SomeActionReturningValueTaskAsync()) is ValueTask vt)
await vt;
Steps To Reproduce
Put the following code in any .cs file in Visual Studio
public class Foo
{
public ValueTask FrobAsync() => default;
public static async ValueTask FrobFooAsync(Foo? foo)
{
if (foo?.FrobAsync() is ValueTask vt)
await vt;
}
}
Expected behavior
CA2012 should not be emitted as long as the ValueTask is only consumed once and not otherwise escapes.
Actual behavior
CA2012 is emitted at the "." of foo?.FrobAsync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment