dotnet / roslyn-analyzers

MIT License
1.59k stars 465 forks source link

Spurious CA2012 on lhs of pattern match #7159

Open poizan42 opened 8 months ago

poizan42 commented 8 months ago

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()

Cyberboss commented 4 weeks ago

I suspect this is similar to this notice that plagues my codebase wherever I use my custom WhenAll for ValueTasks. (It accepts a params ValueTask[])

image

public ValueTask DisposeAsync()
    => ValueTaskExtensions.WhenAll(
        RestClient.DisposeAsync(),
        GraphQLClient.DisposeAsync());