Skip to content

Commit e7d4ca0

Browse files
authored
Merge pull request #3451 from retailcoder/ObjectVariableNotSet
"Object variable not set" inspection enhancements: assignments to Nothing are now taken into consideration.
2 parents ba138e2 + 8d6c5ae commit e7d4ca0

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

Rubberduck.Inspections/Concrete/ObjectVariableNotSetInspection.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,20 @@ 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);
47-
return (reference.IsAssignment && letStmtContext != null);
48+
49+
return reference.IsAssignment && (letStmtContext != null
50+
|| allrefs.Where(r => r.IsAssignment).All(r => ParserRuleContextHelper.GetParent<VBAParser.SetStmtContext>(r.Context)?.expression()?.GetText().Equals(Tokens.Nothing, StringComparison.InvariantCultureIgnoreCase) ?? false));
4851
}
4952
}
5053
}

RubberduckTests/Inspections/ObjectVariableNotSetInspectionTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@ namespace RubberduckTests.Inspections
1111
[TestClass]
1212
public class ObjectVariableNotSetInspectionTests
1313
{
14+
[TestMethod]
15+
[TestCategory("Inspections")]
16+
public void ObjectVariableNotSet_OnlyAssignedToNothing_ReturnsResult()
17+
{
18+
var expectResultCount = 1;
19+
var input =
20+
@"
21+
Private Sub DoSomething()
22+
Dim target As Object
23+
target.DoSomething ' error 91
24+
Set target = Nothing
25+
End Sub
26+
";
27+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
28+
}
29+
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+
1447
[TestMethod]
1548
[TestCategory("Inspections")]
1649
public void ObjectVariableNotSet_GivenIndexerObjectAccess_ReturnsNoResult()

0 commit comments

Comments
 (0)