/
AssignmentNotUsedInspection.cs
323 lines (281 loc) · 15.4 KB
/
AssignmentNotUsedInspection.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;
using Rubberduck.CodeAnalysis.Inspections.Abstract;
using Rubberduck.CodeAnalysis.Inspections.Extensions;
using Rubberduck.Inspections.CodePathAnalysis;
using Rubberduck.Inspections.CodePathAnalysis.Extensions;
using Rubberduck.Inspections.CodePathAnalysis.Nodes;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.VBA.DeclarationCaching;
using Rubberduck.VBEditor;
namespace Rubberduck.CodeAnalysis.Inspections.Concrete
{
/// <summary>
/// Warns about a local variable that is assigned and never read.
/// Or, warns about a local variable that is assigned and then re-assigned
/// before using the previous value.
/// </summary>
/// <why>
/// An assignment that is never used is a meaningless statement that was likely
/// used by an execution path in a prior version of the module.
/// </why>
/// <example hasResult="true">
/// <module name="Module1" type="Standard Module">
/// <![CDATA[
/// Public Sub DoSomething(ByRef value As Long)
/// Dim localVar As Long
/// localVar = 12 ' assignment never used
/// Dim otherVar As Long
/// otherVar = 12
/// value = otherVar * value
/// End Sub
/// ]]>
/// <example hasResult="true">
/// <module name="Module1" type="Standard Module">
/// <![CDATA[
/// Public Sub DoSomething()
/// Dim localVar As Long
/// localVar = 12 ' assignment is redundant
/// localVar = 34
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="Module1" type="Standard Module">
/// <![CDATA[
/// Public Function DoSomething(ByVal value As Long) As Long
/// Dim localVar As Long
/// localVar = 12
/// localVar = localVar + value 'variable is re-assigned, but the prior assigned value is read at least once first.
/// DoSomething = localVar
/// End Function
/// ]]>
/// </module>
/// </example>
internal sealed class AssignmentNotUsedInspection : IdentifierReferenceInspectionBase
{
private readonly Walker _walker;
public AssignmentNotUsedInspection(IDeclarationFinderProvider declarationFinderProvider, Walker walker)
: base(declarationFinderProvider)
{
_walker = walker;
}
protected override IEnumerable<IdentifierReference> ReferencesInModule(QualifiedModuleName module, DeclarationFinder finder)
{
var localNonArrayVariables = finder.Members(module, DeclarationType.Variable)
.Where(declaration => !declaration.IsArray
&& !declaration.ParentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Module));
return localNonArrayVariables
.Where(declaration => !declaration.IsIgnoringInspectionResultFor(AnnotationName))
.SelectMany(d => FindUnusedAssignmentReferences(d, _walker));
}
protected override bool IsResultReference(IdentifierReference reference, DeclarationFinder finder)
{
return !(IsAssignmentOfNothing(reference)
|| IsPotentiallyUsedViaJump(reference, finder));
}
protected override string ResultDescription(IdentifierReference reference)
{
return Description;
}
private static IEnumerable<IdentifierReference> FindUnusedAssignmentReferences(Declaration localVariable, Walker walker)
{
if (!localVariable.References.Any(rf => rf.IsAssignment))
{
return Enumerable.Empty<IdentifierReference>();
}
//Consider static local variables used if they are referenced anywhere within their procedure
if (localVariable.References.Any(r => !r.IsAssignment) && IsStatic(localVariable))
{
return Enumerable.Empty<IdentifierReference>();
}
var tree = walker.GenerateTree(localVariable.ParentScopeDeclaration.Context, localVariable);
var allAssignmentsAndReferences = tree.Nodes(new[] { typeof(AssignmentNode), typeof(ReferenceNode) })
.Where(node => localVariable.References.Contains(node.Reference))
.ToList();
var unusedAssignmentNodes = allAssignmentsAndReferences.Any(n => n is ReferenceNode)
? FindUnusedAssignmentNodes(tree, localVariable, allAssignmentsAndReferences)
: allAssignmentsAndReferences.OfType<AssignmentNode>();
return unusedAssignmentNodes.Where(n => !IsDescendentOfNeverFlagNode(n))
.Select(n => n.Reference);
}
private static IEnumerable<AssignmentNode> FindUnusedAssignmentNodes(INode node, Declaration localVariable, IEnumerable<INode> allAssignmentsAndReferences)
{
var assignmentExprNodes = node.Nodes(new[] { typeof(AssignmentExpressionNode) })
.Where(n => localVariable.References.Contains(n.Children.FirstOrDefault()?.Reference))
.ToList();
var usedAssignments = new List<AssignmentNode>();
foreach (var refNode in allAssignmentsAndReferences.OfType<ReferenceNode>().Reverse())
{
var assignmentExprNodesWithReference = assignmentExprNodes
.Where(n => n.Nodes(new[] { typeof(ReferenceNode) })
.Contains(refNode));
var assignmentsPrecedingReference = assignmentExprNodesWithReference.Any()
? assignmentExprNodes.TakeWhile(n => n != assignmentExprNodesWithReference.LastOrDefault())
?.LastOrDefault()
?.Nodes(new[] { typeof(AssignmentNode) })
: allAssignmentsAndReferences.TakeWhile(n => n != refNode)
.OfType<AssignmentNode>();
if (assignmentsPrecedingReference?.Any() ?? false)
{
usedAssignments.Add(assignmentsPrecedingReference.LastOrDefault() as AssignmentNode);
}
}
return allAssignmentsAndReferences.OfType<AssignmentNode>().Except(usedAssignments);
}
private static bool IsDescendentOfNeverFlagNode(AssignmentNode assignment)
{
return assignment.TryGetAncestorNode<BranchNode>(out _)
|| assignment.TryGetAncestorNode<LoopNode>(out _);
}
private static bool IsAssignmentOfNothing(IdentifierReference reference)
{
return reference.IsSetAssignment
&& reference.Context.Parent is VBAParser.SetStmtContext setStmtContext
&& setStmtContext.expression().GetText().Equals(Tokens.Nothing);
}
/// <summary>
/// Filters false positive result references due to GoTo and Resume statements. e.g.,
/// An ErrorHandler block that branches execution to a location where the asignment may be used.
/// </summary>
/// <remarks>
/// Filters Assignment references that meet the following conditions:
/// 1. Reference precedes a GoTo or Resume statement that branches execution to a line before the
/// assignment reference, AND
/// 2. A non-assignment reference is present on a line that is:
/// a) At or below the start of the execution branch, AND
/// b) Above the next ExitStatement line (if one exists) or the end of the procedure
/// </remarks>
private static bool IsPotentiallyUsedViaJump(IdentifierReference resultCandidate, DeclarationFinder finder)
{
if (!resultCandidate.Declaration.References.Any(rf => !rf.IsAssignment)) { return false; }
var labelIdLineNumberPairs = finder.Members(resultCandidate.QualifiedModuleName, DeclarationType.LineLabel)
.Where(label => resultCandidate.ParentScoping.Equals(label.ParentDeclaration))
.ToDictionary(key => key.IdentifierName, v => v.Context.Start.Line);
return JumpStmtPotentiallyUsesVariable<VBAParser.GoToStmtContext>(resultCandidate, labelIdLineNumberPairs)
|| JumpStmtPotentiallyUsesVariable<VBAParser.ResumeStmtContext>(resultCandidate, labelIdLineNumberPairs);
}
private static bool JumpStmtPotentiallyUsesVariable<T>(IdentifierReference resultCandidate, Dictionary<string,int> labelIdLineNumberPairs) where T: ParserRuleContext
{
if (TryGetRelevantJumpContext<T>(resultCandidate, out var jumpStmt))
{
return IsPotentiallyUsedAssignment(jumpStmt, resultCandidate, labelIdLineNumberPairs);
}
return false;
}
private static bool TryGetRelevantJumpContext<T>(IdentifierReference resultCandidate, out T ctxt) where T : ParserRuleContext
{
ctxt = resultCandidate.ParentScoping.Context.GetDescendents<T>()
.Where(descendent => descendent.GetSelection() > resultCandidate.Selection)
.OrderBy(descendent => descendent.GetSelection())
.FirstOrDefault();
return ctxt != null;
}
private static bool IsPotentiallyUsedAssignment<T>(T jumpContext, IdentifierReference resultCandidate, Dictionary<string, int> labelIdLineNumberPairs) where T : ParserRuleContext
{
int? executionBranchLine = null;
switch (jumpContext)
{
case VBAParser.GoToStmtContext gotoStmt:
executionBranchLine = labelIdLineNumberPairs[gotoStmt.expression().GetText()];
break;
case VBAParser.ResumeStmtContext resume:
executionBranchLine = DetermineResumeStmtExecutionBranchLine(resume, resultCandidate, labelIdLineNumberPairs);
break;
default:
executionBranchLine = null;
break;
}
return executionBranchLine.HasValue
? AssignmentIsUsedPriorToExitStmts(resultCandidate, executionBranchLine.Value)
: false;
}
private static bool AssignmentIsUsedPriorToExitStmts(IdentifierReference resultCandidate, int executionBranchLine)
{
if (resultCandidate.Context.Start.Line < executionBranchLine) { return false; }
var procedureExitStmtCtxts = resultCandidate.ParentScoping.Context.GetDescendents<VBAParser.ExitStmtContext>()
.Where(exitCtxt => exitCtxt.EXIT_DO() == null
&& exitCtxt.EXIT_FOR() == null);
var nonAssignmentCtxts = resultCandidate.Declaration.References
.Where(rf => !rf.IsAssignment)
.Select(rf => rf.Context);
var sortedContextsAfterBranch = nonAssignmentCtxts.Concat(procedureExitStmtCtxts)
.Where(ctxt => ctxt.Start.Line >= executionBranchLine)
.OrderBy(ctxt => ctxt.Start.Line)
.ThenBy(ctxt => ctxt.Start.Column);
return !(sortedContextsAfterBranch.FirstOrDefault() is VBAParser.ExitStmtContext);
}
private static int? DetermineResumeStmtExecutionBranchLine(VBAParser.ResumeStmtContext resumeStmt, IdentifierReference resultCandidate, Dictionary<string, int> labelIdLineNumberPairs)
{
var onErrorGotoLabelToLineNumber = resultCandidate.ParentScoping.Context.GetDescendents<VBAParser.OnErrorStmtContext>()
.Where(errorStmtCtxt => IsBranchingOnErrorGoToLabel(errorStmtCtxt))
.ToDictionary(k => k.expression()?.GetText() ?? "No Label", v => v.Start.Line);
var errorHandlerLabelsAndLines = labelIdLineNumberPairs
.Where(pair => onErrorGotoLabelToLineNumber.ContainsKey(pair.Key));
//Labels must be located at the start of a line.
//If the resultCandidate line precedes all error handling related labels,
//a Resume statement cannot be invoked (successfully) for the resultCandidate
if (!errorHandlerLabelsAndLines.Any(kvp => kvp.Value <= resultCandidate.Context.Start.Line))
{
return null;
}
var resumeStmtExpression = resumeStmt.expression()?.GetText();
//For Resume and Resume Next, expression() is null
if (string.IsNullOrEmpty(resumeStmtExpression))
{
var errorHandlerLabelForTheResumeStatement = errorHandlerLabelsAndLines
.Where(kvp => resumeStmt.Start.Line >= kvp.Value)
.OrderBy(kvp => resumeStmt.Start.Line - kvp.Value)
.Select(kvp => kvp.Key)
.FirstOrDefault();
//Since the execution branch line for Resume and Resume Next statements
//is indeterminant by static analysis, the On***GoTo statement
//is used as the execution branch line
return onErrorGotoLabelToLineNumber[errorHandlerLabelForTheResumeStatement];
}
//Resume <label>
return labelIdLineNumberPairs[resumeStmtExpression];
}
private static bool IsBranchingOnErrorGoToLabel(VBAParser.OnErrorStmtContext errorStmtCtxt)
{
var label = errorStmtCtxt.expression()?.GetText();
if (string.IsNullOrEmpty(label))
{
return false;
}
//The VBE will complain about labels other than:
//1. Numerics less than int.MaxValue (VBA: 'Long' max value). '0' returns false because it cause a branch
//2. Or, Any alphanumeric string beginning with a letter (VBE or the Debugger will choke on special characters, spaces, etc).
return !(int.TryParse(label, out var numberLabel) && numberLabel <= 0);
}
//TODO: Add IsStatic member to VariableDeclaration
private static bool IsStatic(Declaration declaration)
{
var ctxt = declaration.Context.GetAncestor<VBAParser.VariableStmtContext>();
if (ctxt?.STATIC() != null)
{
return true;
}
switch (declaration.ParentDeclaration.Context)
{
case VBAParser.FunctionStmtContext func:
return func.STATIC() != null;
case VBAParser.SubStmtContext sub:
return sub.STATIC() != null;
case VBAParser.PropertyLetStmtContext let:
return let.STATIC() != null;
case VBAParser.PropertySetStmtContext set:
return set.STATIC() != null;
case VBAParser.PropertyGetStmtContext get:
return get.STATIC() != null;
default:
return false;
}
}
}
}