-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add documentation for IDE0360, IDE0370, and IDE0380 code style rules #49681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gewarren
merged 7 commits into
main
from
copilot/add-documentation-for-code-style-rules
Nov 10, 2025
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0cf71ba
Initial plan
Copilot ae2e8ad
Add documentation for IDE0360, IDE0370, and IDE0380
Copilot 0db9648
Update examples for IDE0360, IDE0370, and IDE0380 based on Roslyn tests
Copilot 12586f8
Fix IDE0370 example to show the null-forgiving operator
Copilot f924a8e
add to toc and other index
gewarren 1806686
update category
gewarren 3402b37
Apply suggestions from code review
gewarren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| --- | ||
| title: "IDE0360: Simplify property accessor" | ||
| description: "Learn about code analysis rule IDE0360: Simplify property accessor" | ||
| ms.date: 11/08/2025 | ||
| f1_keywords: | ||
| - IDE0360 | ||
| helpviewer_keywords: | ||
| - IDE0360 | ||
| dev_langs: | ||
| - CSharp | ||
| ai-usage: ai-assisted | ||
| --- | ||
| # Simplify property accessor (IDE0360) | ||
|
|
||
| | Property | Value | | ||
| |--------------------------|-----------------------------------------------| | ||
| | **Rule ID** | IDE0360 | | ||
| | **Title** | Simplify property accessor | | ||
| | **Category** | Style | | ||
| | **Subcategory** | Language rules (expression-level preferences) | | ||
| | **Applicable languages** | C# 13+ | | ||
| | **Options** | `csharp_style_prefer_simple_property_accessors` | | ||
|
|
||
| ## Overview | ||
|
|
||
| This rule flags places where a property accessor that directly accesses the `field` keyword (C# 13+) can be simplified. When a property accessor only returns `field` or assigns a value to `field`, it can be simplified to a simple auto-accessor. | ||
|
|
||
| ## Options | ||
|
|
||
| Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format). | ||
|
|
||
| ### csharp_style_prefer_simple_property_accessors | ||
|
|
||
| | Property | Value | Description | | ||
| |-------------------|-----------------------------------------------|-------------------| | ||
| | **Option name** | `csharp_style_prefer_simple_property_accessors` | | | ||
| | **Option values** | `true` | Prefer simplified property accessors | | ||
| | | `false` | Disables the rule | | ||
| | **Default option value** | `true` | | | ||
|
|
||
| ## Example | ||
|
|
||
| ```csharp | ||
| // Code with violations. | ||
| public int Prop | ||
| { | ||
| get { return field; } | ||
| set { field = value; } | ||
| } | ||
|
|
||
| // Fixed code. | ||
| public int Prop { get; set; } | ||
| ``` | ||
|
|
||
| ## 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 IDE0360 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore IDE0360 | ||
| ``` | ||
|
|
||
| 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.IDE0360.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). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| --- | ||
| title: "IDE0370: Remove unnecessary suppression" | ||
| description: "Learn about code analysis rule IDE0370: Remove unnecessary suppression" | ||
| ms.date: 11/08/2025 | ||
| f1_keywords: | ||
| - IDE0370 | ||
| helpviewer_keywords: | ||
| - IDE0370 | ||
| dev_langs: | ||
| - CSharp | ||
| ai-usage: ai-assisted | ||
| --- | ||
| # Remove unnecessary suppression (IDE0370) | ||
|
|
||
| | Property | Value | | ||
| |--------------------------|--------------------------------------------------| | ||
| | **Rule ID** | IDE0370 | | ||
| | **Title** | Remove unnecessary suppression | | ||
| | **Category** | Style | | ||
| | **Subcategory** | Unnecessary code rules (suppression preferences) | | ||
| | **Applicable languages** | C# | | ||
| | **Options** | None | | ||
|
|
||
| ## Overview | ||
|
|
||
| This rule identifies unnecessary nullable warning suppressions using the [null-forgiving operator](../../../csharp/language-reference/operators/null-forgiving.md) (`!`). The null-forgiving operator tells the compiler that the value is not null, which suppresses warnings for nullable reference types. However, when the compiler can already determine that a value is not null, the null-forgiving operator is unnecessary and can be removed. | ||
|
|
||
| ## Example | ||
|
|
||
| ```csharp | ||
| // Code with violations. | ||
| #nullable enable | ||
|
|
||
| void ProcessValue() | ||
| { | ||
| List<string> names = new()!; | ||
| } | ||
|
|
||
| // Fixed code. | ||
| #nullable enable | ||
|
|
||
| void ProcessValue() | ||
| { | ||
| List<string> names = new(); // No suppression needed. | ||
| } | ||
| ``` | ||
|
|
||
| ## 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 IDE0370 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore IDE0370 | ||
| ``` | ||
|
|
||
| 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.IDE0370.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). | ||
|
|
||
| ## See also | ||
|
|
||
| - [Nullable reference types](../../../csharp/nullable-references.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| title: "IDE0380: Remove unnecessary 'unsafe' modifier" | ||
| description: "Learn about code analysis rule IDE0380: Remove unnecessary 'unsafe' modifier" | ||
| ms.date: 11/08/2025 | ||
| f1_keywords: | ||
| - IDE0380 | ||
| helpviewer_keywords: | ||
| - IDE0380 | ||
| dev_langs: | ||
| - CSharp | ||
| ai-usage: ai-assisted | ||
| --- | ||
| # Remove unnecessary `unsafe` modifier (IDE0380) | ||
|
|
||
| | Property | Value | | ||
| |--------------------------|-----------------------------------------------| | ||
| | **Rule ID** | IDE0380 | | ||
| | **Title** | Remove unnecessary `unsafe` modifier | | ||
| | **Category** | Style | | ||
| | **Subcategory** | Unnecessary code rules (modifier preferences) | | ||
| | **Applicable languages** | C# | | ||
| | **Options** | None | | ||
|
|
||
| ## Overview | ||
|
|
||
| This rule identifies code blocks, methods, types, or other declarations marked with the `unsafe` modifier that don't actually contain any unsafe operations. The `unsafe` modifier allows the use of pointers and other unsafe code features, but when those features aren't being used, the modifier is unnecessary and should be removed for code clarity. | ||
|
|
||
| ## Example | ||
|
|
||
| ```csharp | ||
| // Code with violations. | ||
|
|
||
| // Unnecessary, no unsafe operations. | ||
| unsafe class MyClass | ||
| { | ||
| public void Method() | ||
| { | ||
| var x = 5; | ||
| } | ||
| } | ||
|
|
||
| // Unnecessary, no unsafe operations. | ||
| unsafe void ProcessData(int value) | ||
| { | ||
| Console.WriteLine(value); | ||
| } | ||
|
|
||
| // Fixed code. | ||
| class MyClass | ||
| { | ||
| public void Method() | ||
| { | ||
| var x = 5; | ||
| } | ||
| } | ||
|
|
||
| void ProcessData(int value) | ||
| { | ||
| Console.WriteLine(value); | ||
| } | ||
|
|
||
| // Example where 'unsafe' is needed. | ||
| unsafe class ValidUsage | ||
| { | ||
| int* pointer; // Pointer type requires 'unsafe'. | ||
| } | ||
| ``` | ||
|
|
||
| ## 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 IDE0380 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore IDE0380 | ||
| ``` | ||
|
|
||
| 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.IDE0380.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). | ||
|
|
||
| ## See also | ||
|
|
||
| - [Unsafe code, pointer types, and function pointers](../../../csharp/language-reference/unsafe-code.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.