Skip to content

Commit 04a5ecd

Browse files
authored
Add IDE0320 and IDE0330 (#43386)
1 parent 9816c99 commit 04a5ecd

File tree

7 files changed

+191
-11
lines changed

7 files changed

+191
-11
lines changed

docs/fundamentals/code-analysis/style-rules/ide0250.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ dev_langs:
1313
---
1414
# Struct can be made 'readonly' (IDE0250)
1515

16-
| Property | Value |
17-
|--------------------------|-----------------------------------------------|
18-
| **Rule ID** | IDE0250 |
19-
| **Title** | Struct can be made 'readonly' |
20-
| **Category** | Style |
21-
| **Subcategory** | Unnecessary code rules (modifier preferences) |
22-
| **Applicable languages** | C# |
23-
| **Options** | `csharp_style_prefer_readonly_struct` |
16+
| Property | Value |
17+
|--------------------------|---------------------------------------|
18+
| **Rule ID** | IDE0250 |
19+
| **Title** | Struct can be made 'readonly' |
20+
| **Category** | Style |
21+
| **Subcategory** | Language rules (modifier preferences) |
22+
| **Applicable languages** | C# |
23+
| **Options** | `csharp_style_prefer_readonly_struct` |
2424

2525
## Overview
2626

docs/fundamentals/code-analysis/style-rules/ide0251.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dev_langs:
1818
| **Rule ID** | IDE0251 |
1919
| **Title** | Member can be made 'readonly' |
2020
| **Category** | Style |
21-
| **Subcategory** | Unnecessary code rules (modifier preferences) |
21+
| **Subcategory** | Language rules (modifier preferences) |
2222
| **Applicable languages** | C# 8+ |
2323
| **Options** | `csharp_style_prefer_readonly_struct_member` |
2424

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: "IDE0320: Make anonymous function static"
3+
description: "Learn about code analysis rule IDE0320: Make anonymous function static"
4+
ms.date: 11/08/2024
5+
f1_keywords:
6+
- IDE0320
7+
helpviewer_keywords:
8+
- IDE0320
9+
dev_langs:
10+
- CSharp
11+
---
12+
# Make anonymous function static (IDE0320)
13+
14+
| Property | Value |
15+
|--------------------------|-------------------------------------------|
16+
| **Rule ID** | IDE0320 |
17+
| **Title** | Make anonymous function static |
18+
| **Category** | Style |
19+
| **Subcategory** | Language rules (modifier preferences) |
20+
| **Applicable languages** | C# |
21+
| **Options** | `csharp_prefer_static_anonymous_function` |
22+
23+
## Overview
24+
25+
This style rule flags [anonymous functions](../../../csharp/language-reference/operators/lambda-expressions.md) that can be marked `static`.
26+
27+
## Options
28+
29+
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).
30+
31+
### csharp_prefer_static_anonymous_function
32+
33+
| Property | Value | Description |
34+
|--------------------------|-----------------------------------------|--------------------------------------------------|
35+
| **Option name** | csharp_prefer_static_anonymous_function | |
36+
| **Option values** | `true` | Prefer anonymous functions to be marked `static` |
37+
| | `false` | Disables the rule |
38+
| **Default option value** | `true` | |
39+
40+
## Example
41+
42+
```csharp
43+
// Code with violations.
44+
M(x => x + 1);
45+
M(delegate (int x) { return x + 1; });
46+
47+
void M(Func<int, int> f) { }
48+
```
49+
50+
```csharp
51+
// Fixed code.
52+
M(static x => x + 1);
53+
M(static delegate (int x) { return x + 1; });
54+
55+
void M(Func<int, int> f) { }
56+
```
57+
58+
## Suppress a warning
59+
60+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
61+
62+
```csharp
63+
#pragma warning disable IDE0320
64+
// The code that's violating the rule is on this line.
65+
#pragma warning restore IDE0320
66+
```
67+
68+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
69+
70+
```ini
71+
[*.{cs,vb}]
72+
dotnet_diagnostic.IDE0320.severity = none
73+
```
74+
75+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
76+
77+
```ini
78+
[*.{cs,vb}]
79+
dotnet_analyzer_diagnostic.category-Style.severity = none
80+
```
81+
82+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
83+
84+
## See also
85+
86+
- [Static anonymous functions](/dotnet/csharp/language-reference/proposals/csharp-9.0/static-anonymous-functions)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: "IDE0330: Prefer 'System.Threading.Lock'"
3+
description: "Learn about code analysis rule IDE0330: Prefer 'System.Threading.Lock'"
4+
ms.date: 11/08/2024
5+
f1_keywords:
6+
- IDE0330
7+
helpviewer_keywords:
8+
- IDE0330
9+
dev_langs:
10+
- CSharp
11+
---
12+
# Prefer 'System.Threading.Lock' (IDE0330)
13+
14+
| Property | Value |
15+
|--------------------------|-----------------------------------------|
16+
| **Rule ID** | IDE0330 |
17+
| **Title** | Prefer 'System.Threading.Lock' |
18+
| **Category** | Style |
19+
| **Subcategory** | Language rules (code-block preferences) |
20+
| **Applicable languages** | C# 13+ |
21+
| **Options** | `csharp_prefer_system_threading_lock` |
22+
23+
## Overview
24+
25+
This rule flags places where you can use the .NET 9+ type <xref:System.Threading.Lock?displayProperty=fullName> instead of an `object` lock.
26+
27+
## Options
28+
29+
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).
30+
31+
### csharp_prefer_system_threading_lock
32+
33+
| Property | Value | Description |
34+
|--------------------------|-------------------------------------|--------------------------------------|
35+
| **Option name** | csharp_prefer_system_threading_lock | |
36+
| **Option values** | `true` | Prefer `System.Threading.Lock` locks |
37+
| | `false` | Disables the rule |
38+
| **Default option value** | `true` | |
39+
40+
## Example
41+
42+
```csharp
43+
// Code with violations.
44+
private object _gate = new object();
45+
46+
void M()
47+
{
48+
lock (_gate) { }
49+
}
50+
```
51+
52+
```csharp
53+
// Fixed code.
54+
private Lock _gate = new Lock();
55+
56+
void M()
57+
{
58+
lock (_gate) { }
59+
}
60+
```
61+
62+
## Suppress a warning
63+
64+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
65+
66+
```csharp
67+
#pragma warning disable IDE0330
68+
// The code that's violating the rule is on this line.
69+
#pragma warning restore IDE0330
70+
```
71+
72+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
73+
74+
```ini
75+
[*.{cs,vb}]
76+
dotnet_diagnostic.IDE0330.severity = none
77+
```
78+
79+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
80+
81+
```ini
82+
[*.{cs,vb}]
83+
dotnet_analyzer_diagnostic.category-Style.severity = none
84+
```
85+
86+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).

docs/fundamentals/code-analysis/style-rules/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The following table list all the code-style rules by ID and [options](../code-st
8989
> | [IDE0059](ide0059.md) | Remove unnecessary value assignment | [csharp_style_unused_value_assignment_preference](ide0059.md#csharp_style_unused_value_assignment_preference)<br/> [visual_basic_style_unused_value_assignment_preference](ide0059.md#visual_basic_style_unused_value_assignment_preference) |
9090
> | [IDE0060](ide0060.md) | Remove unused parameter | [dotnet_code_quality_unused_parameters](ide0060.md#dotnet_code_quality_unused_parameters) |
9191
> | [IDE0061](ide0061.md) | Use expression body for local functions | [csharp_style_expression_bodied_local_functions](ide0061.md#csharp_style_expression_bodied_local_functions) |
92-
> | [IDE0062](ide0062.md) | Make local function static | [csharp_prefer_static_local_function](ide0062.md#csharp_prefer_static_local_function) |
92+
> | [IDE0062](ide0062.md) | Make local function `static` | [csharp_prefer_static_local_function](ide0062.md#csharp_prefer_static_local_function) |
9393
> | [IDE0063](ide0063.md) | Use simple `using` statement | [csharp_prefer_simple_using_statement](ide0063.md#csharp_prefer_simple_using_statement) |
9494
> | [IDE0064](ide0064.md) | Make struct fields writable | |
9595
> | [IDE0065](ide0065.md) | `using` directive placement | [csharp_using_directive_placement](ide0065.md#csharp_using_directive_placement) |
@@ -139,6 +139,8 @@ The following table list all the code-style rules by ID and [options](../code-st
139139
> | [IDE0303](ide0303.md) | Use collection expression for `Create()` | [dotnet_style_prefer_collection_expression](ide0303.md#dotnet_style_prefer_collection_expression) |
140140
> | [IDE0304](ide0304.md) | Use collection expression for builder | [dotnet_style_prefer_collection_expression](ide0304.md#dotnet_style_prefer_collection_expression) |
141141
> | [IDE0305](ide0305.md) | Use collection expression for fluent | [dotnet_style_prefer_collection_expression](ide0305.md#dotnet_style_prefer_collection_expression) |
142+
> | [IDE0320](ide0320.md) | Make anonymous function `static` | [csharp_prefer_static_anonymous_function](ide0320.md#csharp_prefer_static_anonymous_function) |
143+
> | [IDE0330](ide0330.md) | Prefer 'System.Threading.Lock' | [csharp_prefer_system_threading_lock](ide0330.md#csharp_prefer_system_threading_lock) |
142144
> | [IDE1005](ide1005.md) | Use conditional delegate call | [csharp_style_conditional_delegate_call](ide1005.md#csharp_style_conditional_delegate_call) |
143145
> | [IDE1006](naming-rules.md) | Naming styles | |
144146

docs/fundamentals/code-analysis/style-rules/language-rules.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Code-style language and unnecessary code rules
33
description: Learn about the different code-style rules for using C# and Visual Basic language constructs and for finding unnecessary code.
4-
ms.date: 02/15/2024
4+
ms.date: 11/08/2024
55
helpviewer_keywords:
66
- language code style rules [EditorConfig]
77
- language rules
@@ -95,6 +95,7 @@ C# style rules:
9595
- [Convert to top-level statements (IDE0210)](ide0210.md)
9696
- [Convert to 'Program.Main' style program (IDE0211)](ide0211.md)
9797
- [Use primary constructor (IDE0290)](ide0290.md)
98+
- [Prefer 'System.Threading.Lock' (IDE0330)](ide0330.md)
9899

99100
### Expression-bodied members
100101

@@ -199,6 +200,7 @@ C# style rules:
199200
- [Make struct fields writable (IDE0064)](ide0064.md)
200201
- [Struct can be made 'readonly' (IDE0250)](ide0250.md)
201202
- [Member can be made 'readonly' (IDE0251)](ide0251.md)
203+
- [Make anonymous function static (IDE0320)](ide0320.md)
202204

203205
### New-line preferences
204206

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,10 @@ items:
16481648
href: ../../fundamentals/code-analysis/style-rules/ide0304.md
16491649
- name: IDE0305
16501650
href: ../../fundamentals/code-analysis/style-rules/ide0305.md
1651+
- name: IDE0320
1652+
href: ../../fundamentals/code-analysis/style-rules/ide0320.md
1653+
- name: IDE0330
1654+
href: ../../fundamentals/code-analysis/style-rules/ide0330.md
16511655
- name: IDE1005
16521656
href: ../../fundamentals/code-analysis/style-rules/ide1005.md
16531657
- name: Miscellaneous rules

0 commit comments

Comments
 (0)