GU0006

November 24, 2021 ยท View on GitHub

Use nameof

TopicValue
IdGU0006
SeverityHidden
EnabledTrue
CategoryGu.Analyzers.Correctness
CodeStringLiteralExpressionAnalyzer

Description

Use nameof.

Motivation

Using nameof is more refactoring friendly than string literals. In the following snippet nameof is preferable for parameter name.

public class C
{
    public void M(object value)
    {
        if (value is null)
        {
            throw new ArgumentNullException(""value"");
        }
    }
}

How to fix violations

Use the cod fix or manually change it to:

public class C
{
    public void M(object value)
    {
        if (value is null)
        {
            throw new ArgumentNullException(nameof(value));
        }
    }
}

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable GU0006 // Use nameof
Code violating the rule here
#pragma warning restore GU0006 // Use nameof

Or put this at the top of the file to disable all instances.

#pragma warning disable GU0006 // Use nameof

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness", 
    "GU0006:Use nameof", 
    Justification = "Reason...")]