-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathDisposeMethodAnalyzer.cs
More file actions
138 lines (121 loc) · 6.32 KB
/
DisposeMethodAnalyzer.cs
File metadata and controls
138 lines (121 loc) · 6.32 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
namespace IDisposableAnalyzers;
using System.Collections.Immutable;
using System.Linq;
using Gu.Roslyn.AnalyzerExtensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class DisposeMethodAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
Descriptors.IDISP009IsIDisposable,
Descriptors.IDISP010CallBaseDispose,
Descriptors.IDISP018CallSuppressFinalizeSealed,
Descriptors.IDISP019CallSuppressFinalizeVirtual,
Descriptors.IDISP020SuppressFinalizeThis,
Descriptors.IDISP021DisposeTrue,
Descriptors.IDISP023ReferenceTypeInFinalizerContext);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(c => Handle(c), SyntaxKind.MethodDeclaration);
}
private static void Handle(SyntaxNodeAnalysisContext context)
{
if (!context.IsExcludedFromAnalysis() &&
context is { ContainingSymbol: IMethodSymbol { IsStatic: false, ReturnsVoid: true, Name: "Dispose" } method, Node: MethodDeclarationSyntax methodDeclaration })
{
if (method is { DeclaredAccessibility: Accessibility.Public, Parameters.Length: 0 } &&
method.GetAttributes().Length == 0)
{
if (!method.ExplicitInterfaceImplementations.Any() &&
method.ContainingType is { IsRefLikeType: false } &&
!IsInterfaceImplementation(method))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.IDISP009IsIDisposable, methodDeclaration.Identifier.GetLocation()));
}
if (ShouldCallBase(SymbolAndDeclaration.Create(method, methodDeclaration), context))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.IDISP010CallBaseDispose, methodDeclaration.Identifier.GetLocation()));
}
if (GC.SuppressFinalize.Find(methodDeclaration, context.SemanticModel, context.CancellationToken) is { Argument: { Expression: { } expression } argument })
{
if (!expression.IsKind(SyntaxKind.ThisExpression))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.IDISP020SuppressFinalizeThis, argument.GetLocation()));
}
}
else if (method.ContainingType.TryFindFirstMethod(x => x.MethodKind == MethodKind.Destructor, out _))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.IDISP018CallSuppressFinalizeSealed, methodDeclaration.Identifier.GetLocation()));
}
else if (method.ContainingType.TryFindFirstMethod(x => DisposeMethod.IsVirtualDispose(x), out _))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.IDISP019CallSuppressFinalizeVirtual, methodDeclaration.Identifier.GetLocation()));
}
if (DisposeBool.Find(methodDeclaration) is { Argument: { Expression: { } } isDisposing } &&
!isDisposing.Expression.IsKind(SyntaxKind.TrueLiteralExpression))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.IDISP021DisposeTrue, isDisposing.GetLocation()));
}
}
if (method.Parameters.TrySingle(out var parameter) &&
parameter.Type == KnownSymbols.Boolean)
{
if (ShouldCallBase(SymbolAndDeclaration.Create(method, methodDeclaration), context))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.IDISP010CallBaseDispose, methodDeclaration.Identifier.GetLocation(), parameter.Name));
}
using var walker = FinalizerContextWalker.Borrow(methodDeclaration, context.SemanticModel, context.CancellationToken);
foreach (var node in walker.UsedReferenceTypes)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.IDISP023ReferenceTypeInFinalizerContext, node.GetLocation()));
}
}
}
}
private static bool IsInterfaceImplementation(IMethodSymbol method)
{
if (method.ContainingType.TypeKind == TypeKind.Interface)
{
return true;
}
foreach (var @interface in method.ContainingType.AllInterfaces)
{
foreach (var member in @interface.GetMembers())
{
if (member is IMethodSymbol { DeclaredAccessibility: Accessibility.Public, ReturnsVoid: true, Name: "Dispose", Parameters.Length: 0 })
{
return true;
}
}
}
return false;
}
private static bool ShouldCallBase(SymbolAndDeclaration<IMethodSymbol, MethodDeclarationSyntax> method, SyntaxNodeAnalysisContext context)
{
if (method is { Symbol: { IsOverride: true, OverriddenMethod: { IsAbstract: false } overridden } } &&
DisposeMethod.FindBaseCall(method.Declaration, context.SemanticModel, context.CancellationToken) is null)
{
if (overridden.DeclaringSyntaxReferences.Length == 0)
{
return true;
}
using var disposeWalker = DisposeWalker.Borrow(overridden, context.SemanticModel, context.CancellationToken);
foreach (var disposeCall in disposeWalker.Invocations)
{
if (disposeCall.FindDisposed(context.SemanticModel, context.CancellationToken) is { } disposed &&
context.SemanticModel.TryGetSymbol(disposed, context.CancellationToken, out var disposedSymbol) &&
FieldOrProperty.TryCreate(disposedSymbol, out var fieldOrProperty) &&
!DisposableMember.IsDisposed(fieldOrProperty, method.Symbol, context.SemanticModel, context.CancellationToken))
{
return true;
}
}
}
return false;
}
}