Back to Devexpress

CRR0031 - The returned Task is null

coderushforroslyn-119688-static-code-analysis-analyzers-library-crr0031-the-returned-task-is-null.md

latest1.2 KB
Original Source

CRR0031 - The returned Task is null

  • Feb 28, 2025

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.

csharp
Task<string> DemoMethodAsync(CancellationToken token) {
    DoThings();
    return null;
}
vb
Private Function DemoMethodAsync(token As CancellationToken) As Task(Of String)
    DoThings()
    Return Nothing
End Function

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

csharp
Task<string> DemoMethodAsync(CancellationToken token) {
    DoThings();
    return Task.FromResult<string>(null);
}
vb
Private Function DemoMethodAsync(ByVal token As CancellationToken) As Task(Of String)
    DoThings()
    Return Task.FromResult(Of String)(Nothing)
End Function

See Also

Suppress Analyzers