Methods that create disposable object(s) and that don't call Dispose()
Enumerates methods creating disposable object(s) without calling any Dispose() method. Not a rule: this is acceptable when the objects are disposed elsewhere. Easily refactored to target specific disposable types or caller methods, and turned into a rule.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Methods that create disposable object(s) and that don't call Dispose()</Name>
// <Id>ND1303:MethodsThatCreateDisposableObjectAndThatDontCallDispose</Id>
// Just uncomment this to transform this code query into a code rule.
// warnif count > 0
// Several IDisposable types can be found if several .NET Fx are referenced.
let iDisposables = ThirdParty.Types.WithFullName("System.IDisposable")
where iDisposables.Any() // in case the code base doesn't use at all System.IDisposable
// Build sequences of disposableTypes and disposeMethods
let disposableTypes = Types.ThatImplementAny(iDisposables).Concat(iDisposables)
let disposeMethods = disposableTypes.ChildMethods().WithName("Dispose()").ToHashSetEx()
// -> You can refine this code query by assigning to disposableTypesToLookAfter something like:
// disposableTypes.WithFullNameIn("Namespace.TypeName1", "Namespace.TypeName2", ...)
let disposableTypesToLookAfter = disposableTypes
// -> You can refine this code query by assigning to methodsToLookAfter something like:
// Application.Assemblies.WithNameLike("Asm").ChildMethods()
let methodsToLookAfter = Application.Methods
// Enumerate methods that create any disposable type, without calling Dispose()
from m in methodsToLookAfter.ThatCreateAny(disposableTypesToLookAfter )
where !m.MethodsCalled.Intersect(disposeMethods).Any()
select new {
m,
disposableObjectsCreated = disposableTypes.Where(t => m.CreateA(t)),
m.MethodsCalled,
//Debt = 10.ToMinutes().ToDebt(),
//Severity = Severity.Low
}
//<Expl>
// The *{0}* instantiates disposable types without calling any *Dispose()* method.
// Disposable type{1?:s} instantiated: *{1}*
//</Expl>
//<Desc>
// Enumerates methods creating disposable object(s) without calling any
// Dispose() method. Not a rule: this is acceptable when the objects are
// disposed elsewhere. Easily refactored to target specific disposable types
// or caller methods, and turned into a rule.
//</Desc>
Open in the interactive Rules Explorer · All default rules
Design: all 16 items
- Avoid custom delegates ND1300
- Types with disposable instance fields must be disposable ND1301
- Disposable types with unmanaged resources should declare finalizer ND1302
- Methods that create disposable object(s) and that don't call Dispose() ND1303
- Disposable Types Must Unsubscribe Events ND1315
- Classes that are candidate to be turned into structures ND1304
- Avoid namespaces with few types ND1305
- Nested types should not be visible ND1306
- Declare types in namespaces ND1307
- Empty static constructor can be discarded ND1308
- Instances size shouldn't be too big ND1309
- Attribute classes should be sealed ND1310
- Don't use obsolete types, methods or fields ND1311
- Do implement methods that throw NotImplementedException ND1312
- Override equals and operator equals on value types ND1313
- Boxing/unboxing should be avoided ND1314