Skip to content

Commit c6f25a4

Browse files
committed
Make FunctionReturnValueDiscarded inspections correctly deal with output lists
1 parent 5d7369a commit c6f25a4

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/FunctionReturnValueAlwaysDiscardedInspection.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ private static bool IsCalledAsProcedure(ParserRuleContext context)
144144
? methodCall
145145
: context;
146146
var memberAccessParent = ownFunctionCallExpression.GetAncestor<VBAParser.MemberAccessExprContext>();
147-
return memberAccessParent == null;
147+
if (memberAccessParent != null)
148+
{
149+
return false;
150+
}
151+
152+
//If we are in an output list, the value is used somewhere in defining the argument.
153+
var outputListParent = context.GetAncestor<VBAParser.OutputListContext>();
154+
return outputListParent == null;
148155
}
149156

150157
protected override string ResultDescription(Declaration declaration)

Rubberduck.CodeAnalysis/Inspections/Concrete/FunctionReturnValueDiscardedInspection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ private static bool IsCalledAsProcedure(ParserRuleContext context)
8282
return false;
8383
}
8484

85-
return true;
85+
//If we are in an output list, the value is used somewhere in defining the argument.
86+
var outputListParent = context.GetAncestor<VBAParser.OutputListContext>();
87+
return outputListParent == null;
8688
}
8789

8890
protected override string ResultDescription(IdentifierReference reference)

RubberduckTests/Inspections/FunctionReturnValueAlwaysDiscardedInspectionTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,23 @@ End Sub
323323
Assert.AreEqual(0, InspectionResultsForStandardModule(code).Count());
324324
}
325325

326+
[Test]
327+
[Category("Inspections")]
328+
[Category("Unused Value")]
329+
public void OutputListFunctionCall_DoesNotReturnResult()
330+
{
331+
const string code = @"
332+
Public Function Foo(ByVal bar As String) As Integer
333+
Foo = 42
334+
End Function
335+
336+
Public Sub Baz()
337+
Debug.Print Foo(""Test"")
338+
End Sub
339+
";
340+
Assert.AreEqual(0, InspectionResultsForStandardModule(code).Count());
341+
}
342+
326343
[Test]
327344
[Category("Inspections")]
328345
[Category("Unused Value")]

RubberduckTests/Inspections/FunctionReturnValueDiscardedInspectionTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,23 @@ End Sub
281281
Assert.AreEqual(0, InspectionResultsForStandardModule(code).Count());
282282
}
283283

284+
[Test]
285+
[Category("Inspections")]
286+
[Category("Unused Value")]
287+
public void FunctionReturnValueDiscarded_DoesNotReturnResult_OutputListFunctionCall()
288+
{
289+
const string code = @"
290+
Public Function Foo(ByVal bar As String) As Integer
291+
Foo = 42
292+
End Function
293+
294+
Public Sub Baz()
295+
Debug.Print Foo(""Test"")
296+
End Sub
297+
";
298+
Assert.AreEqual(0, InspectionResultsForStandardModule(code).Count());
299+
}
300+
284301
[Test]
285302
[Category("Inspections")]
286303
[Category("Unused Value")]

0 commit comments

Comments
 (0)