-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EnsureDebugDisposeFinalizerRule.cs
95 lines (85 loc) · 2.77 KB
/
EnsureDebugDisposeFinalizerRule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2010, SIL International. All Rights Reserved.
// <copyright from='2010' to='2010' company='SIL International'>
// Copyright (c) 2010, SIL International. All Rights Reserved.
// Distributable under the terms of either the Common Public License or the
// GNU Lesser General Public License, as specified in the LICENSING.txt file.
// </copyright>
using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Gendarme.Framework;
using Gendarme.Framework.Rocks;
using Gendarme.Framework.Engines;
using Gendarme.Framework.Helpers;
namespace SIL.Gendarme.Rules.DebugDispose
{
/// <summary>
/// This rule will fire for types which implement <c>System.IDisposable</c> but do not define a finalizer.
/// </summary>
/// <example>
/// Bad example:
/// <code>
/// class NoFinalizer: IDisposable
/// {
/// public void Dispose()
/// {
/// Dispose(true);
/// GC.SuppressFinalize(this);
/// }
/// }
/// </code>
/// </example>
/// <example>
/// Good example:
/// <code>
/// class HasFinalizer: IDisposable
/// {
/// #IF DEBUG
/// ~HasFinalizer ()
/// {
/// Dispose(false)
/// }
/// #endif
/// public void Dispose()
/// {
/// Dispose(true);
/// GC.SuppressFinalize(this);
/// }
/// protected virtual void Dispose(bool disposing)
/// {
/// System.Diagnostics.Debug.WriteLineIf(!disposing, "****** Missing Dispose() call for " + GetType().Name + ". ****** ");
/// }
/// }
/// </code>
/// </example>
[Problem ("This type implements IDisposable but doesn't have a finalizer.")]
[Solution ("When debugging dispose issues add a finalizer, calling Dispose(false), to trigger debug output.")]
public class EnsureDebugDisposeFinalizerRule: Rule, ITypeRule
{
private static readonly MethodSignature Dispose = new MethodSignature("Dispose",
"System.Void", new[] { "System.Boolean"});
#region ITypeRule implementation
public RuleResult CheckType(TypeDefinition type)
{
// rule applies only to types and interfaces
if (type.IsEnum || type.IsDelegate() || type.IsGeneratedCode() ||
type.IsValueType || type.IsInterface)
return RuleResult.DoesNotApply;
// rule only applies to type that implements IDisposable
if (!type.Implements("System", "IDisposable"))
return RuleResult.DoesNotApply;
// rule only applies to type that doesn't derive from class that implements IDisposable
if (type.BaseType.Implements("System", "IDisposable"))
return RuleResult.DoesNotApply;
// rule only applies if type has a Dispose(bool) method
if (!type.HasMethod(Dispose))
return RuleResult.DoesNotApply;
// no problem if a finalizer is found
if (type.HasMethod(MethodSignatures.Finalize))
return RuleResult.Success;
Runner.Report(type, Severity.Medium, Confidence.High);
return RuleResult.Failure;
}
#endregion
}
}