Skip to content

Commit af8bf50

Browse files
authored
Merge pull request #2074 from Hosch250/Issue2010
Ignore Len and LenB in UnassignedVariableUsage inspection
2 parents 9247e89 + 3b6e0cc commit af8bf50

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

RetailCoder.VBE/Inspections/UnassignedVariableUsageInspection.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using Antlr4.Runtime;
4+
using Rubberduck.Parsing;
35
using Rubberduck.Parsing.Symbols;
46
using Rubberduck.Parsing.VBA;
57

@@ -27,10 +29,46 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
2729
.SelectMany(declaration => declaration.References)
2830
.Where(usage => !usage.IsInspectionDisabled(AnnotationName));
2931

32+
var lenFunction = BuiltInDeclarations.SingleOrDefault(s => s.Scope == "VBE7.DLL;VBA.Strings.Len");
33+
var lenbFunction = BuiltInDeclarations.SingleOrDefault(s => s.Scope == "VBE7.DLL;VBA.Strings.LenB");
34+
3035
foreach (var issue in usages)
3136
{
32-
yield return new UnassignedVariableUsageInspectionResult(this, issue.Context, issue.QualifiedModuleName, issue.Declaration);
37+
if (DeclarationReferencesContainsReference(lenFunction, issue) ||
38+
DeclarationReferencesContainsReference(lenbFunction, issue))
39+
{
40+
continue;
41+
}
42+
43+
yield return
44+
new UnassignedVariableUsageInspectionResult(this, issue.Context, issue.QualifiedModuleName,
45+
issue.Declaration);
46+
}
47+
}
48+
49+
private bool DeclarationReferencesContainsReference(Declaration parentDeclaration, IdentifierReference issue)
50+
{
51+
if (parentDeclaration == null)
52+
{
53+
return false;
54+
}
55+
56+
var lenUsesIssue = false;
57+
foreach (var reference in parentDeclaration.References)
58+
{
59+
var context = (ParserRuleContext) reference.Context.Parent;
60+
if (context.GetSelection().Contains(issue.Selection))
61+
{
62+
lenUsesIssue = true;
63+
break;
64+
}
65+
}
66+
67+
if (lenUsesIssue)
68+
{
69+
return true;
3370
}
71+
return false;
3472
}
3573
}
3674
}

0 commit comments

Comments
 (0)