Skip to content

Commit 06f5b03

Browse files
authored
Merge pull request #5640 from MDoerner/CountForLoopAssignmentAsVariableUsage
Count 'For' loop variable as self-referencing assignment, removes inspection false positive.
2 parents 4dc9bab + 616999c commit 06f5b03

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/VariableNotUsedInspection.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,21 @@ protected override bool IsResultDeclaration(Declaration declaration, Declaration
5454
{
5555
return !declaration.IsWithEvents
5656
&& declaration.References
57-
.All(reference => reference.IsAssignment);
57+
.All(reference => reference.IsAssignment)
58+
&& !declaration.References.Any(IsForLoopAssignment);
59+
}
60+
61+
private bool IsForLoopAssignment(IdentifierReference reference)
62+
{
63+
if(!reference.IsAssignment)
64+
{
65+
return false;
66+
}
67+
68+
//A For Next loop has the form For expr1 = expr2 (Step expr3) ... Next (expr1)
69+
var relationalOpAncestor = reference.Context.GetAncestor<VBAParser.RelationalOpContext>();
70+
return relationalOpAncestor != null
71+
&& relationalOpAncestor.Parent is VBAParser.ForNextStmtContext;
5872
}
5973

6074
protected override IInspectionResult InspectionResult(Declaration declaration)

RubberduckTests/Inspections/VariableNotUsedInspectionTests.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ Dim var1 As String
9696

9797
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());
9898
}
99-
100-
//https://github.com/rubberduck-vba/Rubberduck/issues/5088
99+
101100
[Test]
102101
[Category("Inspections")]
102+
//See issue #5610 at https://github.com/rubberduck-vba/Rubberduck/issues/5088
103103
public void VariableNotUsed_AssignedButNeverReferenced_ReturnsResult()
104104
{
105105
const string inputCode =
@@ -110,6 +110,37 @@ Dim var1 As String
110110

111111
Assert.AreEqual(1, InspectionResultsForStandardModule(inputCode).Count());
112112
}
113+
114+
[Test]
115+
[Category("Inspections")]
116+
//See issue #5610 at https://github.com/rubberduck-vba/Rubberduck/issues/5610
117+
public void VariableNotUsed_AssignedinForLoop_DoesNotReturnResult()
118+
{
119+
const string inputCode =
120+
@"Sub Foo()
121+
Dim counter As Long
122+
For counter = 1 To 1000
123+
'Try something
124+
Next
125+
End Sub";
126+
127+
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());
128+
}
129+
130+
[Test]
131+
[Category("Inspections")]
132+
public void VariableNotUsed_AssignedinForEachLoop_ReturnsResult()
133+
{
134+
const string inputCode =
135+
@"Sub Foo()
136+
Dim var1 As Variant
137+
Dim coll As Scription.Collection
138+
For Each var1 In coll
139+
Next
140+
End Sub";
141+
142+
Assert.AreEqual(1, InspectionResultsForStandardModule(inputCode).Count());
143+
}
113144

114145
[Test]
115146
[Category("Inspections")]

0 commit comments

Comments
 (0)