Skip to content

Latest commit

 

History

History
77 lines (61 loc) · 1.58 KB

GU0006.md

File metadata and controls

77 lines (61 loc) · 1.58 KB

GU0006

Use nameof

Topic Value
Id GU0006
Severity Hidden
Enabled True
Category Gu.Analyzers.Correctness
Code StringLiteralExpressionAnalyzer

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...")]