Topic | Value |
---|---|
Id | GU0011 |
Severity | Warning |
Enabled | True |
Category | Gu.Analyzers.Correctness |
Code | GU0011DoNotIgnoreReturnValue |
Don't ignore the returned value.
The rule allows ignoring return value for the following:
- StringBuilder.AppendLine() & Append()
- Methods returning this
- Extension methods returning this.
Ignoring return value is a bug when using LINQ and other pure methods. In the following example add creates a new list that is then only used by the garbage collector.
var list = ImmutableList.Create(1);
list.Add(2);
The above example should be:
var list = ImmutableList.Create(1);
list = list.Add(2);
If a non-void method is called for side effects an extension method like this can be used:
var list = new List<int> { 1 };
list.Remove(1).IgnoreReturnValue();
internal static class Ignore
{
// ReSharper disable once UnusedParameter.Global
internal static void IgnoreReturnValue<T>(this T returnValue)
{
}
}
Configure the severity per project, for more info see MSDN.
#pragma warning disable GU0011 // Don't ignore the returned value
Code violating the rule here
#pragma warning restore GU0011 // Don't ignore the returned value
Or put this at the top of the file to disable all instances.
#pragma warning disable GU0011 // Don't ignore the returned value
[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness",
"GU0011:Don't ignore the returned value",
Justification = "Reason...")]