A method that is not async returns a task from inside a using, which disposes the resource before the task ends
What it finds
using (var client = new Client()) { return client.SendAsync(); } returns the task as soon as the call starts it, and leaves the using, disposing the client, while the task still runs. The same holds for a try with a finally. Making the method async and awaiting the task keeps the resource alive until the work is done. The rule reports return of a call whose name ends in Async inside using or try ... finally, or after a using declaration, in a method that is not async.
From the rule's source, task_returned_from_using.rs.
using System.IO;using System.Threading.Tasks; class Files{ Task Copy(string path) { using (var stream = File.OpenRead(path)) { return stream.CopyToAsync(Stream.Null);tx0 reports: the task is returned from inside a using or finally scope, which ends before the task does } } async Task CopyAwaited(string path) { using (var stream = File.OpenRead(path)) { await stream.CopyToAsync(Stream.Null); } } Task Plain(Stream stream) => stream.FlushAsync();} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:task-returned-from-using" = "warning"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:task-returned-from-using] reason="..."tx0 explain tx0/csharp:task-returned-from-using shows the same in the terminal. Found a false positive? Report it.