GU0006
November 24, 2021 ยท View on GitHub
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...")]