Skip to content

Commit 36737a4

Browse files
committed
Do not report variable not used if it has assignments. Closes #2427
1 parent 7ae665a commit 36737a4

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/VariableNotUsedInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
2222
.Where(declaration =>
2323
!declaration.IsWithEvents
2424
&& !IsIgnoringInspectionResultFor(declaration, AnnotationName)
25-
&& declaration.References.All(reference => reference.IsAssignment));
25+
&& !declaration.References.Any());
2626

2727
return declarations.Select(issue =>
2828
new DeclarationInspectionResult(this,

RubberduckTests/Inspections/VariableNotUsedInspectionTests.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,24 @@ Dim var1 As String
148148

149149
[Test]
150150
[Category("Inspections")]
151-
public void InspectionName()
151+
public void VariableUsed_DoesNotReturnResultIfAssigned()
152152
{
153-
const string inspectionName = "VariableNotUsedInspection";
154-
var inspection = new VariableNotUsedInspection(null);
153+
const string inputCode =
154+
@"Function Foo() As Boolean
155+
Dim var1 as String
156+
var1 = ""test""
157+
End Function";
158+
159+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);
160+
using (var state = MockParser.CreateAndParse(vbe.Object))
161+
{
162+
163+
var inspection = new VariableNotUsedInspection(state);
164+
var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);
155165

156-
Assert.AreEqual(inspectionName, inspection.Name);
166+
Assert.AreEqual(0, inspectionResults.Count());
167+
}
157168
}
169+
158170
}
159171
}

0 commit comments

Comments
 (0)