Reputation: 8844
Hence my following code:
[Conditional("DEBUG")]
internal static void WriteGLResult(string methodName, object result,
params object[] args)
{
string message = String.Format(CultureInfo.InvariantCulture, "{0}({1}) = {2}",
methodName, String.Join(", ", args), result);
Write(LogType.Information, Category.GLResult, 2, message);
}
I'm clearly assigning the String.Format()
result to the variable message
. I'm also passing that instance to the Write method afterwards. Yet CA1806 is raised:
Do not ignore method results
'Log.WriteGLResult(string, object, params object[])' calls 'string.Format(IFormatProvider, string, params object[])' but does not use the new string instance that the method returns. Pass the instance as an argument to another method, assign the instance to a variable, or remove the call if it is unnecessary.`
In the called Write
method, I'm also using the string, there named format
, as it's used like this:
[Conditional("DEBUG")]
private static void Write(LogType logType, Category category, int stackTraceFrameSkips,
string format, params object[] args)
{
// ...
string message = String.Format(CultureInfo.InvariantCulture, format, args);
// ...
}
I don't know how to fix this warning. And I doubt CA raises this erroneously.
Can you help me?
Upvotes: 0
Views: 615
Reputation:
Expanding from the comments:
Since Write
is defined as
[Conditional("DEBUG")]
private static void Write(LogType logType, Category category, int stackTraceFrameSkips,
string format, params object[] args)
that means the calls to Write
will be completely removed when the DEBUG
symbol is not defined. The code analysis runs later, and by that time it can determine (correctly) that message
is never used.
For something that neither warns in debug nor in release mode, you might want to move the call to String.Format
directly into the method arguments:
Write(LogType.Information, Category.GLResult, 2,
String.Format(CultureInfo.InvariantCulture, "{0}({1}) = {2}",
methodName, String.Join(", ", args), result));
This should make sure that String.Format
is only called when Write
will be called.
Upvotes: 1