Skip to content

Commit c39cc20

Browse files
committed
closes #3052
1 parent 505a6d5 commit c39cc20

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Rubberduck.Inspections/Concrete/ObjectVariableNotSetInspection.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,22 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
3434

3535
var objectVariableNotSetReferences = referencesRequiringSetAssignment.Where(FlagIfObjectVariableNotSet);
3636

37-
return objectVariableNotSetReferences.Select(reference =>
37+
return objectVariableNotSetReferences
38+
.Select(reference =>
3839
new IdentifierReferenceInspectionResult(this,
39-
string.Format(InspectionsUI.ObjectVariableNotSetInspectionResultFormat, reference.Declaration.IdentifierName),
40-
State,
41-
reference));
40+
string.Format(InspectionsUI.ObjectVariableNotSetInspectionResultFormat, reference.Declaration.IdentifierName),
41+
State, reference));
4242
}
4343

4444
private bool FlagIfObjectVariableNotSet(IdentifierReference reference)
4545
{
46+
var allrefs = reference.Declaration.References;
4647
var letStmtContext = ParserRuleContextHelper.GetParent<VBAParser.LetStmtContext>(reference.Context);
4748
var setStmtContext = ParserRuleContextHelper.GetParent<VBAParser.SetStmtContext>(reference.Context);
4849
var setAssignmentExpression = setStmtContext?.expression()?.GetText();
4950

5051
return reference.IsAssignment && (letStmtContext != null
51-
|| (setAssignmentExpression?.Equals(Tokens.Nothing, StringComparison.InvariantCultureIgnoreCase) ?? false));
52+
|| allrefs.All(r => r.IsAssignment && (setAssignmentExpression?.Equals(Tokens.Nothing, StringComparison.InvariantCultureIgnoreCase) ?? false)));
5253
}
5354
}
5455
}

RubberduckTests/Inspections/ObjectVariableNotSetInspectionTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ End Sub
2727
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
2828
}
2929

30+
[TestMethod]
31+
[TestCategory("Inspections")]
32+
public void ObjectVariableNotSet_AlsoAssignedToNothing_ReturnsNoResult()
33+
{
34+
var expectResultCount = 0;
35+
var input =
36+
@"
37+
Private Sub DoSomething()
38+
Dim target As Object
39+
Set target = New Object
40+
target.DoSomething
41+
Set target = Nothing
42+
End Sub
43+
";
44+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
45+
}
46+
3047
[TestMethod]
3148
[TestCategory("Inspections")]
3249
public void ObjectVariableNotSet_GivenIndexerObjectAccess_ReturnsNoResult()

0 commit comments

Comments
 (0)