Skip to content

Commit 52322bf

Browse files
committed
1 parent a991482 commit 52322bf

File tree

5 files changed

+87
-35
lines changed

5 files changed

+87
-35
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/ObjectVariableNotSetInspection.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected override IEnumerable<IdentifierReference> ReferencesInModule(Qualified
6060
private static IEnumerable<IdentifierReference> FailedLetResolutionAssignments(QualifiedModuleName module, DeclarationFinder finder)
6161
{
6262
return finder.FailedLetCoercions(module)
63-
.Where(reference => reference.IsAssignment);
63+
.Where(reference => reference.IsAssignment && !reference.Declaration.IsArray);
6464
}
6565

6666
private static IEnumerable<IdentifierReference> PossiblyObjectLhsLetAssignmentsWithNonValueOnRhs(QualifiedModuleName module, DeclarationFinder finder)
@@ -89,11 +89,12 @@ private static IEnumerable<IdentifierReference> PossiblyObjectNonSetAssignments(
8989
{
9090
var assignments = finder.IdentifierReferences(module)
9191
.Where(reference => reference.IsAssignment
92+
&& !reference.Declaration.IsArray
9293
&& !reference.IsSetAssignment
9394
&& (reference.IsNonIndexedDefaultMemberAccess
9495
|| Tokens.Variant.Equals(reference.Declaration.AsTypeName, StringComparison.InvariantCultureIgnoreCase)));
9596
var unboundAssignments = finder.UnboundDefaultMemberAccesses(module)
96-
.Where(reference => reference.IsAssignment);
97+
.Where(reference => reference.IsAssignment && !reference.Declaration.IsArray);
9798

9899
return assignments.Concat(unboundAssignments);
99100
}

Rubberduck.Parsing/Symbols/Declaration.cs

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Antlr4.Runtime;
22
using Rubberduck.Parsing.Annotations;
3+
using Rubberduck.Parsing.Annotations.Concrete;
34
using Rubberduck.Parsing.ComReflection;
45
using Rubberduck.Parsing.Grammar;
56
using Rubberduck.VBEditor;
@@ -8,7 +9,6 @@
89
using System.Collections.Generic;
910
using System.Diagnostics;
1011
using System.Linq;
11-
using Rubberduck.Parsing.Annotations.Concrete;
1212

1313
namespace Rubberduck.Parsing.Symbols
1414
{
@@ -117,7 +117,7 @@ public Declaration(
117117
IEnumerable<IParseTreeAnnotation> annotations = null,
118118
Attributes attributes = null)
119119
{
120-
QualifiedName = qualifiedName;
120+
QualifiedName = qualifiedName;
121121
ParentDeclaration = parentDeclaration;
122122
ParentScopeDeclaration = ParentDeclaration;
123123
ParentScope = parentScope ?? string.Empty;
@@ -145,7 +145,7 @@ public Declaration(
145145
ProjectName = IdentifierName;
146146
}
147147

148-
IsArray = isArray;
148+
IsArray = isArray || AsTypeName != null && (AsTypeNameWithoutArrayDesignator.Length == AsTypeName.Length - "()".Length);
149149
AsTypeContext = asTypeContext;
150150
TypeHint = typeHint;
151151
}
@@ -168,7 +168,8 @@ public Declaration(ComEnumeration enumeration, Declaration parent, QualifiedModu
168168
null,
169169
false,
170170
null,
171-
new Attributes()) { }
171+
new Attributes())
172+
{ }
172173

173174
public Declaration(ComStruct structure, Declaration parent, QualifiedModuleName module)
174175
: this(
@@ -188,7 +189,8 @@ public Declaration(ComStruct structure, Declaration parent, QualifiedModuleName
188189
null,
189190
false,
190191
null,
191-
new Attributes()) { }
192+
new Attributes())
193+
{ }
192194

193195
public Declaration(ComEnumerationMember member, Declaration parent, QualifiedModuleName module) : this(
194196
module.QualifyMemberName(member.Name),
@@ -207,7 +209,8 @@ public Declaration(ComEnumerationMember member, Declaration parent, QualifiedMod
207209
null,
208210
false,
209211
null,
210-
new Attributes()) { }
212+
new Attributes())
213+
{ }
211214

212215
public Declaration(ComField field, Declaration parent, QualifiedModuleName module)
213216
: this(
@@ -227,7 +230,8 @@ public Declaration(ComField field, Declaration parent, QualifiedModuleName modul
227230
null,
228231
false,
229232
null,
230-
new Attributes()) { }
233+
new Attributes())
234+
{ }
231235

232236
public static Declaration GetModuleParent(Declaration declaration)
233237
{
@@ -289,8 +293,8 @@ public string DescriptionString
289293
{
290294
string literalDescription;
291295

292-
var memberAttribute = Attributes.SingleOrDefault(a =>
293-
a.Name == Attributes.MemberAttributeName("VB_Description", IdentifierName) ||
296+
var memberAttribute = Attributes.SingleOrDefault(a =>
297+
a.Name == Attributes.MemberAttributeName("VB_Description", IdentifierName) ||
294298
a.Name == Attributes.MemberAttributeName("VB_VarDescription", IdentifierName));
295299

296300
if (memberAttribute != null)
@@ -323,10 +327,10 @@ public string DescriptionString
323327

324328
private static string CorrectlyFormatedDescription(string literalDescription)
325329
{
326-
if (string.IsNullOrEmpty(literalDescription)
327-
|| literalDescription.Length < 2
330+
if (string.IsNullOrEmpty(literalDescription)
331+
|| literalDescription.Length < 2
328332
|| literalDescription[0] != '"'
329-
|| literalDescription[literalDescription.Length -1] != '"')
333+
|| literalDescription[literalDescription.Length - 1] != '"')
330334
{
331335
return literalDescription;
332336
}
@@ -350,7 +354,7 @@ private bool IsObjectOrObjectArray
350354
{
351355
get
352356
{
353-
if (AsTypeName == Tokens.Object
357+
if (AsTypeName == Tokens.Object
354358
|| (AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.ClassModule) ?? false))
355359
{
356360
return true;

Rubberduck.Parsing/Symbols/ParameterDeclaration.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using System.Collections.Concurrent;
2-
using System.Collections.Generic;
3-
using System.Linq;
41
using Antlr4.Runtime;
52
using Rubberduck.Parsing.Annotations;
63
using Rubberduck.Parsing.Binding;
74
using Rubberduck.Parsing.ComReflection;
85
using Rubberduck.Parsing.Grammar;
96
using Rubberduck.Parsing.VBA.Extensions;
107
using Rubberduck.VBEditor;
8+
using System.Collections.Concurrent;
9+
using System.Collections.Generic;
10+
using System.Linq;
1111

1212
namespace Rubberduck.Parsing.Symbols
1313
{
@@ -39,7 +39,7 @@ public ParameterDeclaration(QualifiedMemberName qualifiedName,
3939
null,
4040
null,
4141
Selection.Home,
42-
isArray,
42+
isArray || isParamArray,
4343
asTypeContext,
4444
false)
4545
{
@@ -78,7 +78,7 @@ public ParameterDeclaration(QualifiedMemberName qualifiedName,
7878
context,
7979
null,
8080
selection,
81-
isArray,
81+
isArray || isParamArray,
8282
asTypeContext,
8383
isUserDefined)
8484
{

RubberduckTests/Grammar/ResolverTests.cs

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading;
51
using NUnit.Framework;
62
using Rubberduck.InternalApi.Extensions;
3+
using Rubberduck.Parsing.Annotations.Concrete;
74
using Rubberduck.Parsing.Symbols;
85
using Rubberduck.Parsing.VBA;
9-
using RubberduckTests.Mocks;
10-
using Rubberduck.Parsing.Annotations;
11-
using Rubberduck.Parsing.Annotations.Concrete;
126
using Rubberduck.VBEditor;
137
using Rubberduck.VBEditor.SafeComWrappers;
148
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
9+
using RubberduckTests.Mocks;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using System.Threading;
1514

1615
namespace RubberduckTests.Grammar
1716
{
@@ -120,6 +119,23 @@ End Sub
120119
}
121120
}
122121

122+
[Category("Resolver")]
123+
[Test]
124+
public void ParamArrayParameter_IsArray()
125+
{
126+
var code = @"Option Explicit
127+
128+
Public Sub Test(ParamArray Values)
129+
End Sub
130+
";
131+
using (var state = Resolve(code))
132+
{
133+
var declaration = state.AllUserDeclarations.Single(item => item.DeclarationType == DeclarationType.Parameter && item.IdentifierName == "Values") as ParameterDeclaration;
134+
Assert.IsTrue(declaration.IsParamArray);
135+
Assert.IsTrue(declaration.IsArray);
136+
}
137+
}
138+
123139
[Category("Resolver")]
124140
[Test]
125141
public void OptionalParameterDefaultConstValue_IsReferenceToDeclaredConst()
@@ -1017,7 +1033,7 @@ End Sub
10171033
item.DeclarationType == DeclarationType.Variable
10181034
&& item.IdentifierName == "foo");
10191035

1020-
Assert.AreEqual(1,declaration.References.Count(item =>
1036+
Assert.AreEqual(1, declaration.References.Count(item =>
10211037
item.ParentScoping.DeclarationType == DeclarationType.Procedure
10221038
&& item.ParentScoping.IdentifierName == "DoSomething"
10231039
&& item.IsAssignment));
@@ -2278,7 +2294,7 @@ End Sub
22782294
var printReference = printDeclaration.References.Single();
22792295

22802296
var module = state.DeclarationFinder.AllModules.Single(qmn => qmn.ComponentType == ComponentType.ClassModule);
2281-
var expectedPrintSelection = new QualifiedSelection(module, new Selection(4, 5,4, 10));
2297+
var expectedPrintSelection = new QualifiedSelection(module, new Selection(4, 5, 4, 10));
22822298
var actualPrintSelection = new QualifiedSelection(printReference.QualifiedModuleName, printReference.Selection);
22832299

22842300
Assert.AreEqual(4, referencedDeclaration.References.Count());
@@ -3632,7 +3648,7 @@ End Function
36323648

36333649
Assert.AreEqual(expectedReferencedDeclarationName, actualReferencedDeclarationName);
36343650
Assert.IsTrue(reference.IsIndexedDefaultMemberAccess);
3635-
Assert.AreEqual(2,reference.DefaultMemberRecursionDepth);
3651+
Assert.AreEqual(2, reference.DefaultMemberRecursionDepth);
36363652
}
36373653
}
36383654

@@ -3832,7 +3848,7 @@ End Function
38323848
{
38333849
var module = state.DeclarationFinder.AllModules.First(qmn => qmn.ComponentName == "Module1");
38343850
var defaultMemberAccess = state.DeclarationFinder.UnboundDefaultMemberAccesses(module).First();
3835-
3851+
38363852
var expectedReferencedSelection = new QualifiedSelection(module, selection);
38373853
var actualReferencedSelection = new QualifiedSelection(defaultMemberAccess.QualifiedModuleName, defaultMemberAccess.Selection);
38383854

@@ -5703,7 +5719,7 @@ End Function
57035719
.Single(param => param.IdentifierName.Equals("furtherArgs"));
57045720
var argumentReferences = parameter.ArgumentReferences;
57055721

5706-
var expectedExpressionTexts = new HashSet<string>{"4", "5", "6"};
5722+
var expectedExpressionTexts = new HashSet<string> { "4", "5", "6" };
57075723
var actualExpressionTexts = argumentReferences.Select(reference => reference.Context.GetText()).ToList();
57085724

57095725
var expectedCount = expectedExpressionTexts.Count;
@@ -7334,7 +7350,7 @@ End Sub
73347350
var undeclared = finder.Members(module.QualifiedModuleName)
73357351
.Where(declaration => declaration.IsUndeclared)
73367352
.ToList();
7337-
7353+
73387354
Assert.AreEqual(4, undeclared.Count);
73397355
}
73407356
}
@@ -7402,7 +7418,7 @@ End Sub
74027418
using (var state = Resolve(vbe.Object))
74037419
{
74047420
var finder = state.DeclarationFinder;
7405-
7421+
74067422
var classModule = finder.UserDeclarations(DeclarationType.ClassModule).Single();
74077423
var barDeclaration = finder.Members(classModule.QualifiedModuleName, DeclarationType.Function).Single();
74087424
var barReference = barDeclaration.References.Single();

RubberduckTests/Inspections/ObjectVariableNotSetInspectionTests.cs

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System.Linq;
21
using NUnit.Framework;
32
using Rubberduck.CodeAnalysis.Inspections;
43
using Rubberduck.CodeAnalysis.Inspections.Concrete;
54
using Rubberduck.Parsing.VBA;
65
using Rubberduck.VBEditor;
76
using Rubberduck.VBEditor.SafeComWrappers;
87
using RubberduckTests.Mocks;
8+
using System.Linq;
99

1010
namespace RubberduckTests.Inspections
1111
{
@@ -210,6 +210,37 @@ Dim target As Variant
210210
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
211211
}
212212

213+
[Test]
214+
[Category("Inspections")]
215+
public void ObjectVariableNotSet_GivenObjectArray_ReturnsNoResult()
216+
{
217+
var expectResultCount = 0;
218+
var input =
219+
@"
220+
Private myObjectArray() As Object
221+
222+
Public Property Get ObjectArray() As Object()
223+
ObjectArray = myObjectArray
224+
End Property
225+
226+
227+
Public Sub Test()
228+
229+
Dim oArr1(0) As Object
230+
Set oArr1(0) = New Something
231+
232+
myObjectArray = oArr1
233+
234+
Dim oArr2() As Object
235+
oArr2 = ObjectArray
236+
237+
Debug.Print oArr2(0).Name
238+
239+
End Sub
240+
";
241+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
242+
}
243+
213244
[Test]
214245
[Category("Inspections")]
215246
public void ObjectVariableNotSet_GivenObjectVariableNotSet_Ignored_DoesNotReturnResult()

0 commit comments

Comments
 (0)