Skip to content

Commit f56d159

Browse files
committed
Fix test for assignments inside For Each statements
1 parent 2cd5d86 commit f56d159

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/VariableRequiresSetAssignmentEvaluator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public static bool RequiresSetAssignment(IdentifierReference reference, Rubberdu
4242
}
4343

4444
// For Each iterators are implicitly set.
45-
if (reference.Context.GetAncestor<VBAParser.ForEachStmtContext>() != null)
45+
var letStmtContext = reference.Context.GetAncestor<VBAParser.LetStmtContext>();
46+
if (reference.Context.GetAncestor<VBAParser.ForEachStmtContext>() != null && letStmtContext == null)
4647
{
4748
return false;
4849
}
@@ -60,9 +61,7 @@ public static bool RequiresSetAssignment(IdentifierReference reference, Rubberdu
6061
return parameters != null && parameters.All(p => p.IsOptional);
6162
}
6263

63-
// assigned declaration is a variant. we need to know about the RHS of the assignment.
64-
65-
var letStmtContext = reference.Context.GetAncestor<VBAParser.LetStmtContext>();
64+
// assigned declaration is a variant. we need to know about the RHS of the assignment.
6665
if (letStmtContext == null)
6766
{
6867
// not an assignment

RubberduckTests/Inspections/ObjectVariableNotSetInspectionTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,58 @@ For Each foo In bar
398398
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
399399
}
400400

401+
[Test]
402+
[Category("Inspections")]
403+
public void ObjectVariableNotSet_ForEachObject_ReturnsNoResult()
404+
{
405+
406+
var expectResultCount = 0;
407+
var input =
408+
@"
409+
Private Sub Test()
410+
Dim bar As Object
411+
For Each foo In bar
412+
Next
413+
End Sub";
414+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
415+
}
416+
417+
[Test]
418+
[Category("Inspections")]
419+
public void ObjectVariableNotSet_InsideForEachObject_ReturnsResult()
420+
{
421+
422+
var expectResultCount = 1;
423+
var input =
424+
@"
425+
Private Sub Test()
426+
Dim bar As Variant
427+
Dim baz As Object
428+
For Each foo In bar
429+
baz = foo
430+
Next
431+
End Sub";
432+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
433+
}
434+
435+
[Test]
436+
[Category("Inspections")]
437+
public void ObjectVariableNotSet_InsideForEachSetObject_ReturnsNoResult()
438+
{
439+
440+
var expectResultCount = 0;
441+
var input =
442+
@"
443+
Private Sub Test()
444+
Dim bar As Variant
445+
Dim baz As Object
446+
For Each foo In bar
447+
Set baz = foo
448+
Next
449+
End Sub";
450+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
451+
}
452+
401453
[Test]
402454
[Category("Inspections")]
403455
public void ObjectVariableNotSet_RSet_ReturnsNoResult()

0 commit comments

Comments
 (0)