A class creates a stream, timer, connection or similar resource in a field but does not implement IDisposable

tx0/csharp:disposable-owner-not-disposablerecommendedwarningreliability/logicdepth fastbug

What it finds

A field that holds a disposable resource ties the resource's lifetime to the object. If the class is not disposable itself, nobody can release the resource early: files stay open, timers keep firing and connections stay in use until the finalizer runs, if ever. The rule reports such fields, initialized with new of a well-known disposable type, in classes without a base list; a class with a base type may inherit IDisposable.

From the rule's source, disposable_owner_not_disposable.rs.

Example from the rule's tests
using System.Threading; class Poller{    private readonly CancellationTokenSource stop = new CancellationTokenSource();tx0 reports: Poller owns the CancellationTokenSource in stop but is not disposable    private readonly object gate = new object();} class Owner : System.IDisposable{    private readonly CancellationTokenSource stop = new CancellationTokenSource();     public void Dispose() => stop.Dispose();}
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-owner-not-disposable" = "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-owner-not-disposable] reason="..."

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