A disposable object was instantiated and used, but not disposed. If it was not returned, most likely it should have been disposed. It might not have to be disposed if it was passed as an argument to some other method.
After some discussion (see bellow) we decided to offer a diagnostic only if the object was created inside the same method, and provide a Warning. An idea for a next issue would be to extend the behavior and allow for objects returned, but this is not a consensus right now, and if we ever do it it will probably be just an Information. Again, see bellow for the discussion.
Scenario 1, no returns:
So this:
public void Foo()
{
var d = new D();
//do something with d
}
Becomes this:
public void Foo()
{
using (var d = new D())
{
//do something with d
}
}
Scenario 2: has a return
(this actually became the same as the first one, we were disposing before the return, not anymore, it is here just for completion)
So this:
public void Foo()
{
var d = new D();
if (condition)
{
//do something with d
return;
}
else
{
//do something else with d
return;
}
}
Becomes this:
public void Foo()
{
using (var d = new D())
{
if (condition)
{
//do something with d
return;
}
else
{
//do something else with d
return;
}
}
}
The disposable object should be disposed at the end of the method.
Suggested category: Warning.
Diagnostic id CC0022
Category: Usage
A disposable object was instantiated and used, but not disposed. If it was not returned, most likely it should have been disposed. It might not have to be disposed if it was passed as an argument to some other method.
After some discussion (see bellow) we decided to offer a diagnostic only if the object was created inside the same method, and provide a
Warning. An idea for a next issue would be to extend the behavior and allow for objects returned, but this is not a consensus right now, and if we ever do it it will probably be just anInformation. Again, see bellow for the discussion.Scenario 1, no returns:
So this:
Becomes this:
Scenario 2: has a return
(this actually became the same as the first one, we were disposing before the return, not anymore, it is here just for completion)
So this:
Becomes this:
The disposable object should be disposed at the end of the method.
Suggested category:
Warning.Diagnostic id
CC0022Category:
Usage