Skip to content

Commit 505a6d5

Browse files
committed
fire a result if variable is 'Set' to 'Nothing'.
1 parent 43fb68f commit 505a6d5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Rubberduck.Inspections/Concrete/ObjectVariableNotSetInspection.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
4444
private bool FlagIfObjectVariableNotSet(IdentifierReference reference)
4545
{
4646
var letStmtContext = ParserRuleContextHelper.GetParent<VBAParser.LetStmtContext>(reference.Context);
47-
return (reference.IsAssignment && letStmtContext != null);
47+
var setStmtContext = ParserRuleContextHelper.GetParent<VBAParser.SetStmtContext>(reference.Context);
48+
var setAssignmentExpression = setStmtContext?.expression()?.GetText();
49+
50+
return reference.IsAssignment && (letStmtContext != null
51+
|| (setAssignmentExpression?.Equals(Tokens.Nothing, StringComparison.InvariantCultureIgnoreCase) ?? false));
4852
}
4953
}
5054
}

RubberduckTests/Inspections/ObjectVariableNotSetInspectionTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ 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+
1430
[TestMethod]
1531
[TestCategory("Inspections")]
1632
public void ObjectVariableNotSet_GivenIndexerObjectAccess_ReturnsNoResult()

0 commit comments

Comments
 (0)