Open JorisCleVR opened 11 months ago
Seems like a reasonable enhancement, but might be tricky to implement as you mentioned. We need to handle all different type of collections, and also ensure we only flag those collections which we know have elements instantiated in the containing type (see the Note in https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/CA2213#rule-description). We also need to bail out if the collection is passed outside the type.
@mavasani I'd like to look into this if you could assign this to me.
Analyzer
Diagnostic ID: CA2213
Describe the improvement
CA2213 should be raised when the disposable items are stored in a collection type.
Describe suggestions on how to achieve the rule
When a collection type field is available that stores IDisposable items the rule should be raised when the items in the list are not disposed. I am not sure how easy it is to detect whether items in a list are disposed. Unfortunately a DisposeAll method is not available natively by C# itself, otherwise it could just check if that method was called on the list field.
Additional context
The following part is an adjustment of https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2213
The following snippet shows a type TypeA that implements IDisposable.
The following snippet shows a type TypeB that violates rule CA2223 by declaring a collection type field aListFieldOfADisposableType containing items of disposable type (TypeA) and not calling Dispose on the items in the list.
To fix the violation, call Dispose() on the disposable items of the collection field: