Skip to main content

CRR0031 - The returned Task is null

This analyzer detects the asynchronous methods that return null. A Task-typed method returning null may appear after refactoring and causes the NullReferenceException when the method is called with the await keyword.

Task<string> DemoMethodAsync(CancellationToken token) {
    DoThings();
    return null;
}

You should construct a Task from null and return it to fix this issue.

Task<string> DemoMethodAsync(CancellationToken token) {
    DoThings();
    return Task.FromResult<string>(null);
}