/
ProcedureNotUsedInspection.cs
200 lines (189 loc) · 6.75 KB
/
ProcedureNotUsedInspection.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System.Collections.Generic;
using System.Linq;
using Rubberduck.CodeAnalysis.Inspections.Abstract;
using Rubberduck.CodeAnalysis.Inspections.Extensions;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.VBA.DeclarationCaching;
using Rubberduck.Resources.Inspections;
namespace Rubberduck.CodeAnalysis.Inspections.Concrete
{
/// <summary>
/// Locates procedures that are never invoked from user code.
/// </summary>
/// <why>
/// Unused procedures are dead code that should probably be removed. Note, a procedure may be effectively "not used" in code, but attached to some
/// Shape object in the host document: in such cases the inspection result should be ignored.
/// </why>
/// <remarks>
/// Not all unused procedures can/should be removed: ignore any inspection results for
/// event handler procedures and interface members that Rubberduck isn't recognizing as such.
/// Public procedures of Standard Modules are not flagged by this inspection regardless of
/// the presence or absence of user code references.
/// </remarks>
/// <example hasResult="true">
/// <module name="Module1" type="Standard Module">
/// <![CDATA[
/// Option Explicit
///
/// Private Sub DoSomething()
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="Module1" type="Standard Module">
/// <![CDATA[
/// Option Explicit
///
/// '@Ignore ProcedureNotUsed
/// Private Sub DoSomething()
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="Macros" type="Standard Module">
/// <![CDATA[
/// Option Explicit
///
/// Public Sub DoSomething()
/// 'a public procedure in a standard module may be a macro
/// 'attached to a worksheet Shape or invoked by means other than user code.
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="true">
/// <module name="Class1" type="Class Module">
/// <![CDATA[
/// Option Explicit
///
/// Public Sub DoSomething()
/// End Sub
///
/// Public Sub DoSomethingElse()
/// End Sub
/// ]]>
/// </module>
/// <module name="Module1" type="Standard Module">
/// <![CDATA[
/// Option Explicit
///
/// Public Sub ReferenceOneClass1Procedure()
/// Dim target As Class1
/// Set target = new Class1
/// target.DoSomething
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="Class1" type="Class Module">
/// <![CDATA[
/// Option Explicit
///
/// Public Sub DoSomething()
/// End Sub
///
/// Public Sub DoSomethingElse()
/// End Sub
/// ]]>
/// </module>
/// <module name="Module1" type="Standard Module">
/// <![CDATA[
/// Option Explicit
///
/// Public Sub ReferenceAllClass1Procedures()
/// Dim target As Class1
/// Set target = new Class1
/// target.DoSomething
/// target.DoSomethingElse
/// End Sub
/// ]]>
/// </module>
/// </example>
internal sealed class ProcedureNotUsedInspection : DeclarationInspectionBase
{
public ProcedureNotUsedInspection(IDeclarationFinderProvider declarationFinderProvider)
: base(declarationFinderProvider, ProcedureTypes)
{}
private static readonly DeclarationType[] ProcedureTypes =
{
DeclarationType.Procedure,
DeclarationType.Function,
DeclarationType.LibraryProcedure,
DeclarationType.LibraryFunction,
DeclarationType.Event
};
private static readonly string[] ClassLifeCycleHandlers =
{
"Class_Initialize",
"Class_Terminate"
};
private static readonly string[] DocumentEventHandlerPrefixes =
{
"Chart_",
"Worksheet_",
"Workbook_",
"Document_",
"Application_",
"Session_"
};
protected override bool IsResultDeclaration(Declaration declaration, DeclarationFinder finder)
{
return !declaration.References
.Any(reference => !reference.ParentScoping.Equals(declaration)) // ignore recursive/self-referential calls
&& !finder.FindEventHandlers().Contains(declaration)
&& !IsPublicModuleMember(declaration)
&& !IsClassLifeCycleHandler(declaration)
&& !(declaration is ModuleBodyElementDeclaration member
&& (member.IsInterfaceMember
|| member.IsInterfaceImplementation))
&& !declaration.Annotations
.Any(pta => pta.Annotation is ITestAnnotation)
&& !IsDocumentEventHandler(declaration);
}
/// <remarks>
/// We cannot determine whether exposed members of standard modules are called or not,
/// so we assume they are instead of flagging them as "never called".
/// </remarks>
private static bool IsPublicModuleMember(Declaration procedure)
{
if ((procedure.Accessibility != Accessibility.Implicit
&& procedure.Accessibility != Accessibility.Public))
{
return false;
}
var parent = Declaration.GetModuleParent(procedure);
return parent != null
&& parent.DeclarationType.HasFlag(DeclarationType.ProceduralModule);
}
private static bool IsClassLifeCycleHandler(Declaration procedure)
{
if (!ClassLifeCycleHandlers.Contains(procedure.IdentifierName))
{
return false;
}
var parent = Declaration.GetModuleParent(procedure);
return parent != null
&& parent.DeclarationType.HasFlag(DeclarationType.ClassModule);
}
private static bool IsDocumentEventHandler(Declaration declaration)
{
var declarationName = declaration.IdentifierName;
return DocumentEventHandlerPrefixes
.Any(prefix => declarationName.StartsWith(prefix));
}
protected override string ResultDescription(Declaration declaration)
{
var declarationType = declaration.DeclarationType.ToLocalizedString();
var declarationName = declaration.IdentifierName;
return string.Format(
InspectionResults.IdentifierNotUsedInspection,
declarationType,
declarationName);
}
}
}