A variable declared by using is also disposed by an explicit Dispose() call
What it finds
A using disposes its variable at the end of its scope. Calling Dispose() on it as well disposes it twice; most types tolerate that, but some throw, and the explicit call suggests that the variable is used after it was meant to be released. The rule reports x.Dispose() where x is declared by a using statement or declaration around the call.
From the rule's source, using_variable_disposed.rs.
using System.IO; class Files{ void Copy(string path) { using (var stream = File.OpenRead(path)) { stream.CopyTo(Stream.Null); stream.Dispose();tx0 reports: stream is disposed here and again by its using } var other = File.OpenRead(path); other.Dispose(); }} 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:using-variable-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:using-variable-disposed] reason="..."tx0 explain tx0/csharp:using-variable-disposed shows the same in the terminal. Found a false positive? Report it.