Skip to content

Commit

Permalink
Merge pull request #5362 from MDoerner/FixFunctionReturnValueDiscarde…
Browse files Browse the repository at this point in the history
…dInspection

Handle output lists (Print statements), fixes FunctionReturnValue(Always)DiscardedInspection
  • Loading branch information
retailcoder committed Jan 19, 2020
2 parents f27427f + c6f25a4 commit 3ef11e3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Expand Up @@ -144,7 +144,14 @@ private static bool IsCalledAsProcedure(ParserRuleContext context)
? methodCall
: context;
var memberAccessParent = ownFunctionCallExpression.GetAncestor<VBAParser.MemberAccessExprContext>();
return memberAccessParent == null;
if (memberAccessParent != null)
{
return false;
}

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

protected override string ResultDescription(Declaration declaration)
Expand Down
Expand Up @@ -82,7 +82,9 @@ private static bool IsCalledAsProcedure(ParserRuleContext context)
return false;
}

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

protected override string ResultDescription(IdentifierReference reference)
Expand Down
Expand Up @@ -323,6 +323,23 @@ End Sub
Assert.AreEqual(0, InspectionResultsForStandardModule(code).Count());
}

[Test]
[Category("Inspections")]
[Category("Unused Value")]
public void OutputListFunctionCall_DoesNotReturnResult()
{
const string code = @"
Public Function Foo(ByVal bar As String) As Integer
Foo = 42
End Function
Public Sub Baz()
Debug.Print Foo(""Test"")
End Sub
";
Assert.AreEqual(0, InspectionResultsForStandardModule(code).Count());
}

[Test]
[Category("Inspections")]
[Category("Unused Value")]
Expand Down
Expand Up @@ -281,6 +281,23 @@ End Sub
Assert.AreEqual(0, InspectionResultsForStandardModule(code).Count());
}

[Test]
[Category("Inspections")]
[Category("Unused Value")]
public void FunctionReturnValueDiscarded_DoesNotReturnResult_OutputListFunctionCall()
{
const string code = @"
Public Function Foo(ByVal bar As String) As Integer
Foo = 42
End Function
Public Sub Baz()
Debug.Print Foo(""Test"")
End Sub
";
Assert.AreEqual(0, InspectionResultsForStandardModule(code).Count());
}

[Test]
[Category("Inspections")]
[Category("Unused Value")]
Expand Down

0 comments on commit 3ef11e3

Please sign in to comment.