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;
}
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.
Task<string> DemoMethodAsync(CancellationToken token) {
DoThings();
return Task.FromResult<string>(null);
}
Private Function DemoMethodAsync(ByVal token As CancellationToken) As Task(Of String)
DoThings()
Return Task.FromResult(Of String)(Nothing)
End Function