Skip to content

Commit 3a915a4

Browse files
committed
Add non-undeclared declarations for ReDimed array without dclaration
1 parent 9ee0bf9 commit 3a915a4

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Rubberduck.Parsing/VBA/DeclarationCaching/DeclarationFinder.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,9 @@ public Declaration OnUndeclaredVariable(Declaration enclosingProcedure, string i
974974
{
975975
var annotations = FindAnnotations(enclosingProcedure.QualifiedName.QualifiedModuleName, context.Start.Line)
976976
.Where(annotation => annotation.AnnotationType.HasFlag(AnnotationType.IdentifierAnnotation));
977+
978+
var isReDimVariable = IsContainedInReDimedArrayName(context);
979+
977980
var undeclaredLocal =
978981
new Declaration(
979982
new QualifiedMemberName(enclosingProcedure.QualifiedName.QualifiedModuleName, identifierName),
@@ -988,12 +991,12 @@ public Declaration OnUndeclaredVariable(Declaration enclosingProcedure, string i
988991
context,
989992
null,
990993
context.GetSelection(),
991-
false,
994+
isReDimVariable,
992995
null,
993996
true,
994997
annotations,
995998
null,
996-
true);
999+
!isReDimVariable);
9971000

9981001
var hasUndeclared = _newUndeclared.ContainsKey(enclosingProcedure.QualifiedName);
9991002
if (hasUndeclared)
@@ -1017,6 +1020,13 @@ public Declaration OnUndeclaredVariable(Declaration enclosingProcedure, string i
10171020
return undeclaredLocal;
10181021
}
10191022

1023+
private static bool IsContainedInReDimedArrayName(ParserRuleContext context)
1024+
{
1025+
var enclosingReDimContext = context.GetAncestor<VBAParser.RedimVariableDeclarationContext>();
1026+
return enclosingReDimContext != null
1027+
&& enclosingReDimContext.expression().GetSelection().Contains(context.GetSelection());
1028+
}
1029+
10201030

10211031
public void AddUnboundContext(Declaration parentDeclaration, VBAParser.LExpressionContext context, IBoundExpression withExpression)
10221032
{

RubberduckTests/Inspections/UndeclaredVariableInspectionTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,35 @@ Debug.Print a
8888
}
8989
}
9090

91+
[Test]
92+
[Category("Inspections")]
93+
//ReDim acts as a declaration if the array is not declared already.
94+
//See issue #2522 at https://github.com/rubberduck-vba/Rubberduck/issues/2522
95+
public void UndeclaredVariable_ReturnsNoResultForReDim()
96+
{
97+
const string inputCode =
98+
@"
99+
Sub Test()
100+
Dim bar As Variant
101+
ReDim arr(1 To 42)
102+
bar = arr
103+
End Sub";
104+
105+
var builder = new MockVbeBuilder();
106+
var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected)
107+
.AddComponent("MyClass", ComponentType.ClassModule, inputCode)
108+
.Build();
109+
var vbe = builder.AddProject(project).Build();
110+
111+
using (var state = MockParser.CreateAndParse(vbe.Object))
112+
{
113+
var inspection = new UndeclaredVariableInspection(state);
114+
var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);
115+
116+
Assert.IsFalse(inspectionResults.Any());
117+
}
118+
}
119+
91120
//https://github.com/rubberduck-vba/Rubberduck/issues/2525
92121
[Test]
93122
[Category("Inspections")]

0 commit comments

Comments
 (0)