Skip to content

Spurious CA2012 on lhs of pattern match #7159

Open

Description

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

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