A stream, reader, writer or connection created in a method is never disposed

tx0/csharp:disposable-not-disposedrecommendedwarningreliability/resourcesdepth fastbug

What it finds

var reader = new StreamReader(path); without using keeps the file open until the garbage collector finalizes the reader, which may be never in a long-running process. using var reader = ... closes it at the end of the scope. Reported are locals created with new of common types that hold operating system resources and that are not disposed, returned, stored in a field or passed to another call in the method.

From the rule's source, disposable_not_disposed.rs.

Example from the rule's tests
using System.IO; class Files{    string Read(string path)    {        var reader = new StreamReader(path);tx0 reports: the StreamReader in reader is never disposed        var text = reader.ReadToEnd();        using var other = new StreamReader(path);        var kept = new FileStream(path, FileMode.Open);        Store(kept);        var closed = new StreamWriter(path);        closed.Write(text);        closed.Dispose();        return text + other.ReadToEnd();    }     void Store(Stream s) { }}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:disposable-not-disposed" = "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:disposable-not-disposed] reason="..."

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