A method returns the variable of a using, which is disposed as the method returns

tx0/csharp:disposed-object-returneddefaulterrorreliability/resourcesdepth fastbug

What it finds

using var stream = Open(); return stream; disposes the stream on the way out, so the caller receives an object that can no longer be used. The same holds for a variable declared in a using (...) statement and returned from inside it. Either the method gives up ownership, and drops the using, or it finishes the work itself.

From the rule's source, disposed_object_returned.rs.

Example from the rule's tests
using System.IO; class Files{    Stream Open(string path)    {        using var stream = File.OpenRead(path);        return stream;tx0 reports: stream is disposed by its using as the method returns it    }     Stream Keep(string path)    {        var stream = File.OpenRead(path);        return stream;    }     string Read(string path)    {        using (var reader = new StreamReader(path))        {            return reader.ReadToEnd();        }    }}
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:disposed-object-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:disposed-object-returned] reason="..."

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