Skip to content

Add analyzer: Call async methods when in an async method #45661

@jeffhandley

Description

@jeffhandley

AB#1245428

Background and Motivation

The VSTHRD103 analyzer exists and it is broadly valuable. We should promote this analyzer to be part of the standard set of analyzers that ship with the .NET SDK.

Examples of patterns that are flagged by this analyzer

Task DoAsync()
{
    file.Read(buffer, 0, 10);
}

All methods where an Async-suffixed equivalent exists will produce this warning
when called from a Task-returning method.
In addition, calling Task.Wait(), Task<T>.Result or Task.GetAwaiter().GetResult()
will produce this warning.

Solution

Await the async version of the method:

async Task DoAsync()
{
    await file.ReadAsync(buffer, 0, 10);
}

Metadata

Metadata

Labels

Cost:SWork that requires one engineer up to 1 weekPriority:1Work that is critical for the release, but we could probably ship withoutapi-approvedAPI was approved in API review, it can be implementedarea-System.Threading.Taskscode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixer

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions