-
Notifications
You must be signed in to change notification settings - Fork 222
/
DisposeNotImplementingDispose.cs
170 lines (145 loc) · 7.63 KB
/
DisposeNotImplementingDispose.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace SonarAnalyzer.Rules.CSharp
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class DisposeNotImplementingDispose : SonarDiagnosticAnalyzer
{
private const string DiagnosticId = "S2953";
private const string MessageFormat = "Either implement 'IDisposable.Dispose', or totally rename this method to prevent confusion.";
private const string DisposeMethodName = "Dispose";
private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterSymbolAction(
c =>
{
var declaredSymbol = (INamedTypeSymbol)c.Symbol;
// ref structs cannot inherit from the IDisposable interface
if (declaredSymbol.IsRefStruct())
{
return;
}
var disposeMethod = c.Compilation.SpecialTypeMethod(SpecialType.System_IDisposable, "Dispose");
if (disposeMethod == null)
{
return;
}
var mightImplementDispose = new HashSet<IMethodSymbol>();
var namedDispose = new HashSet<IMethodSymbol>();
var methods = declaredSymbol.GetMembers(DisposeMethodName).OfType<IMethodSymbol>();
foreach (var method in methods)
{
CollectMethodsNamedAndImplementingDispose(method, disposeMethod, namedDispose, mightImplementDispose);
}
var disposeMethodsCalledFromDispose = new HashSet<IMethodSymbol>();
CollectInvocationsFromDisposeImplementation(disposeMethod, c.Compilation, mightImplementDispose, disposeMethodsCalledFromDispose);
ReportDisposeMethods(c, namedDispose.Except(mightImplementDispose).Where(m => !disposeMethodsCalledFromDispose.Contains(m)));
},
SymbolKind.NamedType);
private static void CollectInvocationsFromDisposeImplementation(IMethodSymbol disposeMethod, Compilation compilation,
HashSet<IMethodSymbol> mightImplementDispose,
HashSet<IMethodSymbol> disposeMethodsCalledFromDispose)
{
foreach (var method in mightImplementDispose
.Where(method => MethodIsDisposeImplementation(method, disposeMethod)))
{
var methodDeclarations = method.DeclaringSyntaxReferences
.Select(x => new NodeAndModel<MethodDeclarationSyntax>(compilation.GetSemanticModel(x.SyntaxTree), x.GetSyntax() as MethodDeclarationSyntax))
.Where(x => x.Node != null);
var methodDeclaration = methodDeclarations.FirstOrDefault(m => m.Node.HasBodyOrExpressionBody());
if (methodDeclaration == null)
{
continue;
}
var invocations = methodDeclaration.Node.DescendantNodes().OfType<InvocationExpressionSyntax>();
foreach (var invocation in invocations)
{
CollectDisposeMethodsCalledFromDispose(invocation, methodDeclaration.Model, disposeMethodsCalledFromDispose);
}
}
}
private static void CollectDisposeMethodsCalledFromDispose(InvocationExpressionSyntax invocationExpression,
SemanticModel semanticModel,
HashSet<IMethodSymbol> disposeMethodsCalledFromDispose)
{
if (!invocationExpression.IsOnThis())
{
return;
}
if (!(semanticModel.GetSymbolInfo(invocationExpression).Symbol is IMethodSymbol invokedMethod) || invokedMethod.Name != DisposeMethodName)
{
return;
}
disposeMethodsCalledFromDispose.Add(invokedMethod);
}
private static void ReportDisposeMethods(SonarSymbolReportingContext context, IEnumerable<IMethodSymbol> disposeMethods)
{
foreach (var disposeMethod in disposeMethods)
{
foreach (var location in disposeMethod.Locations)
{
context.ReportIssue(Rule.CreateDiagnostic(context.Compilation, location, disposeMethod.PartialImplementationPart?.Locations ?? Enumerable.Empty<Location>()));
}
}
}
private static void CollectMethodsNamedAndImplementingDispose(IMethodSymbol methodSymbol,
IMethodSymbol disposeMethod,
HashSet<IMethodSymbol> namedDispose,
HashSet<IMethodSymbol> mightImplementDispose)
{
if (methodSymbol.Name != DisposeMethodName)
{
return;
}
namedDispose.Add(methodSymbol);
if (methodSymbol.IsOverride
|| MethodIsDisposeImplementation(methodSymbol, disposeMethod)
|| MethodMightImplementDispose(methodSymbol))
{
mightImplementDispose.Add(methodSymbol);
}
}
private static bool MethodIsDisposeImplementation(IMethodSymbol methodSymbol, IMethodSymbol disposeMethod) =>
methodSymbol.Equals(methodSymbol.ContainingType.FindImplementationForInterfaceMember(disposeMethod));
private static bool MethodMightImplementDispose(IMethodSymbol declaredMethodSymbol)
{
var containingType = declaredMethodSymbol.ContainingType;
if (containingType.BaseType is { Kind: SymbolKind.ErrorType })
{
return true;
}
var interfaces = containingType.AllInterfaces;
foreach (var @interface in interfaces)
{
if (@interface.Kind == SymbolKind.ErrorType)
{
return true;
}
var interfaceMethods = @interface.GetMembers().OfType<IMethodSymbol>();
if (interfaceMethods.Any(interfaceMethod => declaredMethodSymbol.Equals(containingType.FindImplementationForInterfaceMember(interfaceMethod))))
{
return true;
}
}
return false;
}
}
}