Skip to content

Commit

Permalink
add ide0280
Browse files Browse the repository at this point in the history
  • Loading branch information
gewarren committed Jul 27, 2023
1 parent 729fa36 commit dd0636f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
71 changes: 71 additions & 0 deletions docs/fundamentals/code-analysis/style-rules/ide0280.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: "IDE0280: Use 'nameof'"
description: "Learn about code analysis rule IDE0280: Use 'nameof'"
ms.date: 07/26/2023
ms.topic: reference
f1_keywords:
- IDE0280
helpviewer_keywords:
- IDE0280
dev_langs:
- CSharp
---
# Use 'nameof' (IDE0280)

| Property | Value |
|--------------------------|-----------------------------------------------|
| **Rule ID** | IDE0280 |
| **Title** | Use 'nameof' |
| **Category** | Style |
| **Subcategory** | Language rules (expression-level preferences) |
| **Applicable languages** | C# |

## Overview

This rule flags the use of a literal parameter name instead of the [`nameof` expression](../../../csharp/language-reference/operators/nameof.md) in attributes such as <xref:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute>, <xref:System.Diagnostics.CodeAnalysis.NotNullWhenAttribute>, and <xref:System.Runtime.CompilerServices.CallerArgumentExpressionAttribute> that take a parameter name.

## Options

This rule has no associated code-style options.

## Example

```csharp
// Code with violations.
class C
{
void M([NotNullIfNotNull("input")] string? input) { }
}

// Fixed code.
class C
{
void M([NotNullIfNotNull(nameof(input))] string? input) { }
}
```

## Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

```csharp
#pragma warning disable IDE0280
// The code that's violating the rule is on this line.
#pragma warning restore IDE0280
```

To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).

```ini
[*.{cs,vb}]
dotnet_diagnostic.IDE0280.severity = none
```

To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).

```ini
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
```

For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
1 change: 1 addition & 0 deletions docs/fundamentals/code-analysis/style-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ The following table list all the code-style rules by ID and [options](../code-st
> | [IDE0251](ide0251.md) | Member can be made 'readonly' | [csharp_style_prefer_readonly_struct_member](ide0251.md#csharp_style_prefer_readonly_struct_member) |
> | [IDE0260](ide0078-ide0260.md) | Use pattern matching | [csharp_style_pattern_matching_over_as_with_null_check](ide0078-ide0260.md#csharp_style_pattern_matching_over_as_with_null_check-ide0260) |
> | [IDE0270](ide0029-ide0030-ide0270.md) | Null check can be simplified | [dotnet_style_coalesce_expression](ide0029-ide0030-ide0270.md#dotnet_style_coalesce_expression) |
> | [IDE0280](ide0280.md) | Use `nameof` | |
> | [IDE1005](ide1005.md) | Use conditional delegate call | [csharp_style_conditional_delegate_call](ide1005.md#csharp_style_conditional_delegate_call) |
> | [IDE1006](naming-rules.md) | Naming styles | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ C# style rules:
.NET style rules (C# and Visual Basic):

- [Remove unused parameter (IDE0060)](ide0060.md)
- [Use 'nameof' (IDE0280)](ide0280.md)

### Parentheses preferences

Expand Down
2 changes: 2 additions & 0 deletions docs/navigate/tools-diagnostics/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,8 @@ items:
href: ../../fundamentals/code-analysis/style-rules/ide0078-ide0260.md
- name: IDE0270
href: ../../fundamentals/code-analysis/style-rules/ide0029-ide0030-ide0270.md
- name: IDE0280
href: ../../fundamentals/code-analysis/style-rules/ide0280.md
- name: IDE1005
href: ../../fundamentals/code-analysis/style-rules/ide1005.md
- name: Miscellaneous rules
Expand Down

0 comments on commit dd0636f

Please sign in to comment.