Skip to content

Commit 841f851

Browse files
committed
Make ObjectVariableNotSetInpection only handle single RHS variables in the single variable RHS case
Closes #4318 The problem was that the case picked up the first simple name subexpression, which is quite often the member part of a member access expression. Now, the case verifies first that the simpleNameExpr on the RHS is all there is in the expression.
1 parent 77fa481 commit 841f851

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/VariableRequiresSetAssignmentEvaluator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ public static bool RequiresSetAssignment(IdentifierReference reference, IDeclara
9696
var project = Declaration.GetProjectParent(reference.ParentScoping);
9797
var module = Declaration.GetModuleParent(reference.ParentScoping);
9898

99+
//Covers the case of a single variable on the RHS of the assignment.
99100
var simpleName = expression.GetDescendent<VBAParser.SimpleNameExprContext>();
100-
if (simpleName != null)
101+
if (simpleName != null && simpleName.GetText() == expression.GetText())
101102
{
102103
return declarationFinderProvider.DeclarationFinder.MatchName(simpleName.identifier().GetText())
103104
.Any(d => AccessibilityCheck.IsAccessible(project, module, reference.ParentScoping, d) && d.IsObject);

RubberduckTests/Inspections/ObjectVariableNotSetInspectionTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,24 @@ Dim bar As Variant
483483
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
484484
}
485485

486+
[Test]
487+
[Category("Inspections")]
488+
public void ObjectVariableNotSet_ComplexExpressionOnRHSWithMemberAccess_ReturnsNoResult()
489+
{
490+
491+
var expectResultCount = 0;
492+
var input =
493+
@"
494+
Private Sub Test()
495+
Dim foo As Variant
496+
Dim bar As Collection
497+
Set bar = New Collection
498+
bar.Add ""x"", ""x""
499+
foo = ""Test"" & bar.Item(""x"")
500+
End Sub";
501+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount, new[]{"VBA.4.2"});
502+
}
503+
486504
[Test]
487505
[Category("Inspections")]
488506
public void ObjectVariableNotSet_LSetOnUDT_ReturnsNoResult()

0 commit comments

Comments
 (0)