A method that returns a task returns null, which throws when awaited

tx0/csharp:null-task-returneddefaultwarningreliability/null-safetydepth fastbug

What it finds

Awaiting null throws NullReferenceException. A method declared to return Task or Task<T> that is not async and returns null hands that to its callers; Task.CompletedTask or Task.FromResult give them something to await.

From the rule's source, null_task_returned.rs.

Example from the rule's tests
using System.Threading.Tasks; class Loader{    Task Save(bool skip)    {        if (skip)        {            return null;tx0 reports: returning null from a method that returns Task throws when awaited        }        return Task.CompletedTask;    }     Task<int> Count() => null;tx0 reports: returning null from a method that returns Task<int> throws when awaited     async Task<string> Name()    {        await Task.Yield();        return null;    }     Task<int>? Maybe() => null;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:null-task-returned" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:null-task-returned] reason="..."

tx0 explain tx0/csharp:null-task-returned shows the same in the terminal. Found a false positive? Report it.