Skip to content

Latest commit

 

History

History
82 lines (63 loc) · 1.98 KB

GU0011.md

File metadata and controls

82 lines (63 loc) · 1.98 KB

GU0011

Don't ignore the returned value

Topic Value
Id GU0011
Severity Warning
Enabled True
Category Gu.Analyzers.Correctness
Code GU0011DoNotIgnoreReturnValue

Description

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.

Motivation

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);

How to fix violations

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 severity

Via ruleset file.

Configure the severity per project, for more info see MSDN.

Via #pragma directive.

#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

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness", 
    "GU0011:Don't ignore the returned value", 
    Justification = "Reason...")]