Fork me on GitHub
TypeName DisposableVariableNotDisposedAnalyzer
Check Id CC0022
Category Usage
Severity Warning

Cause

A method has a disposable object (an object created from a class that implements IDisposable) and is not disposing the object through a Dispose call or a using block. The variable cannot be saved to a field or returned.

Example

void Foo()
{
    var m = new MemoryStream();
    //other statements
}

Code fix

A code fix will be presented to you that will transform the code, it will wrap the object creation expression in a using block if the scope allows it, like so:

void Foo()
{
    using (var m = new MemoryStream())
    {
        //other statements
    }
}

If the scope does not allow for a using block it will simply add a Dispose call to the end of the method.

Code fix

Related rules

  • CC0029 - Disposable should call SuppressFinalize
  • CC0032 - Disposable field not disposed (method call)
  • CC0033 - Disposable field not disposed (object creation)

See also

TBD