diff --git a/docs/fundamentals/code-analysis/style-rules/ide0360.md b/docs/fundamentals/code-analysis/style-rules/ide0360.md new file mode 100644 index 0000000000000..8e28ae61771be --- /dev/null +++ b/docs/fundamentals/code-analysis/style-rules/ide0360.md @@ -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 > 0) ? value : throw new ArgumentException(); } +} + +// 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). diff --git a/docs/fundamentals/code-analysis/style-rules/ide0370.md b/docs/fundamentals/code-analysis/style-rules/ide0370.md new file mode 100644 index 0000000000000..020138eba0531 --- /dev/null +++ b/docs/fundamentals/code-analysis/style-rules/ide0370.md @@ -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 names = new()!; +} + +// Fixed code. +#nullable enable + +void ProcessValue() +{ + List 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) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0380.md b/docs/fundamentals/code-analysis/style-rules/ide0380.md new file mode 100644 index 0000000000000..a42dc99114354 --- /dev/null +++ b/docs/fundamentals/code-analysis/style-rules/ide0380.md @@ -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) diff --git a/docs/fundamentals/code-analysis/style-rules/index.md b/docs/fundamentals/code-analysis/style-rules/index.md index fec923289f122..7060e750e061e 100644 --- a/docs/fundamentals/code-analysis/style-rules/index.md +++ b/docs/fundamentals/code-analysis/style-rules/index.md @@ -146,6 +146,9 @@ The following table list all the code-style rules by ID and [options](../code-st > | [IDE0330](ide0330.md) | Prefer 'System.Threading.Lock' | [csharp_prefer_system_threading_lock](ide0330.md#csharp_prefer_system_threading_lock) | > | [IDE0340](ide0340.md) | Use unbound generic type | [csharp_style_prefer_unbound_generic_type_in_nameof](ide0340.md#csharp_style_prefer_unbound_generic_type_in_nameof) | > | [IDE0350](ide0350.md) | Use implicitly typed lambda | [csharp_style_prefer_implicitly_typed_lambda_expression](ide0350.md#csharp_style_prefer_implicitly_typed_lambda_expression) | +> | [IDE0360](ide0360.md) | Simplify property accessor | [csharp_style_prefer_simple_property_accessors](ide0360.md#csharp_style_prefer_simple_property_accessors) | +> | [IDE0370](ide0370.md) | Remove unnecessary suppression | | +> | [IDE0380](ide0380.md) | Remove unnecessary 'unsafe' modifier | | > | [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 | | > | [IDE2000](ide2000.md) | Avoid multiple blank lines | [dotnet_style_allow_multiple_blank_lines_experimental](ide2000.md#dotnet_style_allow_multiple_blank_lines_experimental)† | diff --git a/docs/fundamentals/code-analysis/style-rules/language-rules.md b/docs/fundamentals/code-analysis/style-rules/language-rules.md index bd86cd6744ebd..6ca1f966d419c 100644 --- a/docs/fundamentals/code-analysis/style-rules/language-rules.md +++ b/docs/fundamentals/code-analysis/style-rules/language-rules.md @@ -1,7 +1,7 @@ --- title: Code-style language and unnecessary code rules description: Learn about the different code-style rules for using C# and Visual Basic language constructs and for finding unnecessary code. -ms.date: 03/25/2025 +ms.date: 11/07/2025 helpviewer_keywords: - language code style rules [EditorConfig] - language rules @@ -172,6 +172,7 @@ C# style rules: - [Use collection expression for new (IDE0306)](ide0306.md) - [Use unbound generic type (IDE0340)](ide0340.md) - [Use implicitly typed lambda (IDE0350)](ide0350.md) +- [Simplify property accessor (IDE0360)](ide0360.md) Visual Basic style rules: @@ -205,6 +206,7 @@ C# style rules: - [Struct can be made 'readonly' (IDE0250)](ide0250.md) - [Member can be made 'readonly' (IDE0251)](ide0251.md) - [Make anonymous function static (IDE0320)](ide0320.md) +- [Remove unnecessary `unsafe` modifier (IDE0380)](ide0380.md) ### New-line preferences @@ -259,6 +261,10 @@ C# style rules: - [Remove unnecessary suppression (IDE0079)](ide0079.md) +C# style rules: + +- [Remove unnecessary suppression (null-forgiving operator) (IDE0370)](ide0370.md) + ### `This.` and `me.` preferences .NET style rules (C# and Visual Basic): diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index 3bfea72f9aa3b..ef7767c738afb 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -3874,6 +3874,12 @@ items: href: ../../fundamentals/code-analysis/style-rules/ide0340.md - name: IDE0350 href: ../../fundamentals/code-analysis/style-rules/ide0350.md + - name: IDE0360 + href: ../../fundamentals/code-analysis/style-rules/ide0360.md + - name: IDE0370 + href: ../../fundamentals/code-analysis/style-rules/ide0370.md + - name: IDE0380 + href: ../../fundamentals/code-analysis/style-rules/ide0380.md - name: IDE1005 href: ../../fundamentals/code-analysis/style-rules/ide1005.md - name: IDE2000