dotnet / roslyn-analyzers

MIT License
1.58k stars 465 forks source link

CA1859 does not apply to async methods returning a Task<T> #7262

Open martincostello opened 6 months ago

martincostello commented 6 months ago

Analyzer

Diagnostic ID: CA1859

Describe the improvement

The following method raises CA1859, suggesting that the return type is changed to List<string>:

private static IList<string> GetList()
{
    return new List<string>();
}

The same pattern is however not detected for async methods returning a Task<T>. The following code does not suggest that Task<IList<string>> is changed to Task<List<string>>:

private static async Task<IList<string>> GetListAsync()
{
    await Task.CompletedTask;
    return new List<string>();
}

The above examples are trivialised versions of a real async method where I noticed returning an IList<T> wasn't suggesting the use of a List<T>.

Describe suggestions on how to achieve the rule

Change the rule to consider the real type that is used to create the Task<T>.

Additional context

None.