Skip to content

Commit 3e7c7d2

Browse files
authored
Add missing code-style rules (#30059)
1 parent c1c5bd5 commit 3e7c7d2

File tree

12 files changed

+299
-22
lines changed

12 files changed

+299
-22
lines changed

docs/csharp/language-reference/operators/patterns.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,15 @@ A property pattern is a recursive pattern. That is, you can use any pattern as a
175175

176176
The preceding example uses two features available in C# 9.0 and later: `or` [pattern combinator](#logical-patterns) and [record types](../builtin-types/record.md).
177177

178-
Beginning with C# 10, you can reference nested properties or fields within a property pattern. For example, you can refactor the method from the preceding example into the following equivalent code:
178+
Beginning with C# 10, you can reference nested properties or fields within a property pattern. This is known as an *extended property pattern*. For example, you can refactor the method from the preceding example into the following equivalent code:
179179

180180
:::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="ExtendedPropertyPattern":::
181181

182182
For more information, see the [Property pattern](~/_csharplang/proposals/csharp-8.0/patterns.md#property-pattern) section of the feature proposal note and the [Extended property patterns](~/_csharplang/proposals/csharp-10.0/extended-property-patterns.md) feature proposal note.
183183

184+
> [!TIP]
185+
> You can use the [Simplify property pattern (IDE0170)](../../../fundamentals/code-analysis/style-rules/ide0170.md) style rule to improve code readability by suggesting places to use extended property patterns.
186+
184187
## Positional pattern
185188

186189
Beginning with C# 8.0, you use a *positional pattern* to deconstruct an expression result and match the resulting values against the corresponding nested patterns, as the following example shows:

docs/fundamentals/code-analysis/code-style-rule-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ csharp_preferred_modifier_order = public,private,protected,internal,static,exter
136136
csharp_prefer_braces = true:silent
137137
csharp_style_deconstructed_variable_declaration = true:suggestion
138138
csharp_prefer_simple_default_expression = true:suggestion
139-
csharp_style_pattern_local_over_anonymous_function = true:suggestion
139+
csharp_style_prefer_local_over_anonymous_function = true:suggestion
140140
csharp_style_inlined_variable_declaration = true:suggestion
141141
###############################
142142
# C# Formatting Rules #

docs/fundamentals/code-analysis/style-rules/expression-level-preferences.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The style rules in this section concern the following expression-level preferenc
4242
- [Use range operator (IDE0057)](ide0057.md)
4343
- [Add missing cases to switch expression (IDE0072)](ide0072.md)
4444
- [Simplify 'new' expression (IDE0090)](ide0090.md)
45+
- [Use tuple to swap values (IDE0180)](ide0180.md)
4546

4647
## See also
4748

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ ms.date: 09/30/2020
55
ms.topic: reference
66
f1_keywords:
77
- IDE0039
8-
- csharp_style_pattern_local_over_anonymous_function
8+
- csharp_style_prefer_local_over_anonymous_function
99
helpviewer_keywords:
1010
- IDE0039
11-
- csharp_style_pattern_local_over_anonymous_function
11+
- csharp_style_prefer_local_over_anonymous_function
1212
author: gewarren
1313
ms.author: gewarren
1414
dev_langs:
1515
- CSharp
1616
---
1717
# Use local function instead of lambda (IDE0039)
1818

19-
| Property | Value |
20-
| ------------------------ | ---------------------------------------------------- |
21-
| **Rule ID** | IDE0039 |
22-
| **Title** | Use local function instead of lambda |
23-
| **Category** | Style |
24-
| **Subcategory** | Language rules (expression-level preferences) |
25-
| **Applicable languages** | C# 7.0+ |
26-
| **Options** | `csharp_style_pattern_local_over_anonymous_function` |
19+
| Property | Value |
20+
| ------------------------ | --------------------------------------------------- |
21+
| **Rule ID** | IDE0039 |
22+
| **Title** | Use local function instead of lambda |
23+
| **Category** | Style |
24+
| **Subcategory** | Language rules (expression-level preferences) |
25+
| **Applicable languages** | C# 7.0+ |
26+
| **Options** | `csharp_style_prefer_local_over_anonymous_function` |
2727

2828
## Overview
2929

@@ -33,23 +33,23 @@ This style rule concerns the use of [local functions](../../../csharp/programmin
3333

3434
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).
3535

36-
### csharp_style_pattern_local_over_anonymous_function
36+
### csharp_style_prefer_local_over_anonymous_function
3737

38-
| Property | Value | Description |
39-
| ------------------------ | -------------------------------------------------- | ----------------------------------------------- |
40-
| **Option name** | csharp_style_pattern_local_over_anonymous_function | |
41-
| **Option values** | `true` | Prefer local functions over anonymous functions |
42-
| | `false` | Prefer anonymous functions over local functions |
43-
| **Default option value** | `true` | |
38+
| Property | Value | Description |
39+
| ------------------------ | ------------------------------------------------- | ----------------------------------------------- |
40+
| **Option name** | csharp_style_prefer_local_over_anonymous_function | |
41+
| **Option values** | `true` | Prefer local functions over anonymous functions |
42+
| | `false` | Prefer anonymous functions over local functions |
43+
| **Default option value** | `true` | |
4444

4545
```csharp
46-
// csharp_style_pattern_local_over_anonymous_function = true
46+
// csharp_style_prefer_local_over_anonymous_function = true
4747
int fibonacci(int n)
4848
{
4949
return n <= 1 ? 1 : fibonacci(n-1) + fibonacci(n-2);
5050
}
5151

52-
// csharp_style_pattern_local_over_anonymous_function = false
52+
// csharp_style_prefer_local_over_anonymous_function = false
5353
Func<int, int> fibonacci = null;
5454
fibonacci = (int n) =>
5555
{
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: "IDE0150: Prefer 'null' check over type check"
3+
description: "Learn about code analysis rule IDE0150: Prefer 'null' check over type check"
4+
ms.date: 06/29/2022
5+
ms.topic: reference
6+
f1_keywords:
7+
- IDE0150
8+
- csharp_style_prefer_null_check_over_type_check
9+
helpviewer_keywords:
10+
- IDE0150
11+
- csharp_style_prefer_null_check_over_type_check
12+
author: gewarren
13+
ms.author: gewarren
14+
---
15+
# Prefer 'null' check over type check (IDE0150)
16+
17+
| Property | Value |
18+
|--------------------------|--------------------------------------------------|
19+
| **Rule ID** | IDE0150 |
20+
| **Title** | Prefer `null` check over type check |
21+
| **Category** | Style |
22+
| **Subcategory** | Language rules (null-checking preferences) |
23+
| **Applicable languages** | C# |
24+
| **Options** | `csharp_style_prefer_null_check_over_type_check` |
25+
26+
## Overview
27+
28+
This style rule flags use of the `is {type}` statement when `is not null` can be used instead. Similarly, it flags use of the `is not {type}` statement in favor of `is null`. Using `is null` or `is not null` improves code readability.
29+
30+
## Options
31+
32+
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).
33+
34+
### csharp_style_prefer_null_check_over_type_check
35+
36+
| Property | Value | Description |
37+
|--------------------------|------------------------------------------------|------------------------------------|
38+
| **Option name** | csharp_style_prefer_null_check_over_type_check | |
39+
| **Option values** | `true` | Prefer null check over type check. |
40+
| | `false` | Disables the rule. |
41+
| **Default option value** | `true` | |
42+
43+
## Example
44+
45+
```csharp
46+
// Violates IDE0150.
47+
if (numbers is not IEnumerable<int>) ...
48+
49+
// Fixed code.
50+
if (numbers is null) ...
51+
```
52+
53+
## Suppress a warning
54+
55+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
56+
57+
```csharp
58+
#pragma warning disable IDE0150
59+
// The code that's violating the rule is on this line.
60+
#pragma warning restore IDE0150
61+
```
62+
63+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
64+
65+
```ini
66+
[*.{cs,vb}]
67+
dotnet_diagnostic.IDE0150.severity = none
68+
```
69+
70+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
71+
72+
```ini
73+
[*.{cs,vb}]
74+
dotnet_analyzer_diagnostic.category-Style.severity = none
75+
```
76+
77+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
78+
79+
## See also
80+
81+
- [Null-checking preferences](null-checking-preferences.md)
82+
- [Code style language rules](language-rules.md)
83+
- [Code style rules reference](index.md)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: "IDE0170: Simplify property pattern"
3+
description: "Learn about code analysis rule IDE0170: Simplify property pattern"
4+
ms.date: 06/29/2022
5+
ms.topic: reference
6+
f1_keywords:
7+
- IDE0170
8+
- csharp_style_prefer_extended_property_pattern
9+
helpviewer_keywords:
10+
- IDE0170
11+
- csharp_style_prefer_extended_property_pattern
12+
author: gewarren
13+
ms.author: gewarren
14+
---
15+
# Simplify property pattern (IDE0170)
16+
17+
| Property | Value |
18+
|--------------------------|--------------------------------------------------|
19+
| **Rule ID** | IDE0170 |
20+
| **Title** | Simplify property pattern |
21+
| **Category** | Style |
22+
| **Subcategory** | Language rules (pattern matching preferences) |
23+
| **Applicable languages** | C# |
24+
| **Options** | `csharp_style_prefer_extended_property_pattern` |
25+
26+
## Overview
27+
28+
This style rule flags the use of a nested pattern in a [property pattern](../../../csharp/language-reference/operators/patterns.md#property-pattern). A nested pattern can be simplified to use an extended property pattern in which property subpatterns are used to reference nested members. Extended property patterns improve code readability.
29+
30+
## Options
31+
32+
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).
33+
34+
### csharp_style_prefer_extended_property_pattern
35+
36+
| Property | Value | Description |
37+
|--------------------------|-----------------------------------------------|---------------------------------------|
38+
| **Option name** | csharp_style_prefer_extended_property_pattern | |
39+
| **Option values** | `true` | Prefer the extended property pattern. |
40+
| | `false` | Disables the rule. |
41+
| **Default option value** | `true` | |
42+
43+
## Example
44+
45+
```csharp
46+
public record Point(int X, int Y);
47+
public record Segment(Point Start, Point End);
48+
49+
// Violates IDE0170.
50+
static bool IsEndOnXAxis(Segment segment) =>
51+
segment is { Start: { Y: 0 } } or { End: { Y: 0 } };
52+
53+
// Fixed code.
54+
static bool IsEndOnXAxis(Segment segment) =>
55+
segment is { Start.Y: 0 } or { End.Y: 0 };
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 IDE0170
64+
// The code that's violating the rule is on this line.
65+
#pragma warning restore IDE0170
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.IDE0170.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+
- [Property pattern](../../../csharp/language-reference/operators/patterns.md#property-pattern)
87+
- [Pattern matching preferences](pattern-matching-preferences.md)
88+
- [Code style language rules](language-rules.md)
89+
- [Code style rules reference](index.md)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: "IDE0180: Use tuple to swap values"
3+
description: "Learn about code analysis rule IDE0180: Use tuple to swap values"
4+
ms.date: 06/29/2022
5+
ms.topic: reference
6+
f1_keywords:
7+
- IDE0180
8+
- csharp_style_prefer_tuple_swap
9+
helpviewer_keywords:
10+
- IDE0180
11+
- csharp_style_prefer_tuple_swap
12+
author: gewarren
13+
ms.author: gewarren
14+
---
15+
# Use tuple to swap values (IDE0180)
16+
17+
| Property | Value |
18+
|--------------------------|--------------------------------------------------|
19+
| **Rule ID** | IDE0180 |
20+
| **Title** | Use tuple to swap values |
21+
| **Category** | Style |
22+
| **Subcategory** | Language rules (expression-level preferences) |
23+
| **Applicable languages** | C# |
24+
| **Options** | `csharp_style_prefer_tuple_swap` |
25+
26+
## Overview
27+
28+
This style rule flags code that swaps two values using multiple lines of code instead of using a [tuple](../../../csharp/language-reference/builtin-types/value-tuples.md).
29+
30+
## Options
31+
32+
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).
33+
34+
### csharp_style_prefer_tuple_swap
35+
36+
| Property | Value | Description |
37+
|--------------------------|--------------------------------|------------------------------------------|
38+
| **Option name** | csharp_style_prefer_tuple_swap | |
39+
| **Option values** | `true` | Prefer using a tuple to swap two values. |
40+
| | `false` | Disables the rule. |
41+
| **Default option value** | `true` | |
42+
43+
## Example
44+
45+
```csharp
46+
List<int> numbers = new List<int>() { 5, 6, 4 };
47+
48+
// Violates IDE0180.
49+
int temp = numbers[0];
50+
numbers[0] = numbers[1];
51+
numbers[1] = temp;
52+
53+
// Fixed code.
54+
(numbers[1], numbers[0]) = (numbers[0], numbers[1]);
55+
```
56+
57+
## Suppress a warning
58+
59+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
60+
61+
```csharp
62+
#pragma warning disable IDE0180
63+
// The code that's violating the rule is on this line.
64+
#pragma warning restore IDE0180
65+
```
66+
67+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
68+
69+
```ini
70+
[*.{cs,vb}]
71+
dotnet_diagnostic.IDE0180.severity = none
72+
```
73+
74+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
75+
76+
```ini
77+
[*.{cs,vb}]
78+
dotnet_analyzer_diagnostic.category-Style.severity = none
79+
```
80+
81+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
82+
83+
## See also
84+
85+
- [Expression-level preferences](expression-level-preferences.md)
86+
- [Code style language rules](language-rules.md)
87+
- [Code style rules reference](index.md)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The following table list all the code-style rules by ID and [options](../code-st
7474
> | [IDE0036](ide0036.md) | Order modifiers | [csharp_preferred_modifier_order](ide0036.md#csharp_preferred_modifier_order)<br/> [visual_basic_preferred_modifier_order](ide0036.md#visual_basic_preferred_modifier_order)<br/> |
7575
> | [IDE0037](ide0037.md) | Use inferred member name | [dotnet_style_prefer_inferred_tuple_names](ide0037.md#dotnet_style_prefer_inferred_tuple_names)<br/> [dotnet_style_prefer_inferred_anonymous_type_member_names](ide0037.md#dotnet_style_prefer_inferred_anonymous_type_member_names) |
7676
> | [IDE0038](ide0020-ide0038.md) | Use pattern matching to avoid `is` check followed by a cast (without variable) | [csharp_style_pattern_matching_over_is_with_cast_check](ide0020-ide0038.md#csharp_style_pattern_matching_over_is_with_cast_check) |
77-
> | [IDE0039](ide0039.md) | Use local function instead of lambda | [csharp_style_pattern_local_over_anonymous_function](ide0039.md#csharp_style_pattern_local_over_anonymous_function) |
77+
> | [IDE0039](ide0039.md) | Use local function instead of lambda | [csharp_style_prefer_local_over_anonymous_function](ide0039.md#csharp_style_prefer_local_over_anonymous_function) |
7878
> | [IDE0040](ide0040.md) | Add accessibility modifiers | [dotnet_style_require_accessibility_modifiers](ide0040.md#dotnet_style_require_accessibility_modifiers) |
7979
> | [IDE0041](ide0041.md) | Use is null check | [dotnet_style_prefer_is_null_check_over_reference_equality_method](ide0041.md#dotnet_style_prefer_is_null_check_over_reference_equality_method) |
8080
> | [IDE0042](ide0042.md) | Deconstruct variable declaration | [csharp_style_deconstructed_variable_declaration](ide0042.md#csharp_style_deconstructed_variable_declaration) |
@@ -120,6 +120,9 @@ The following table list all the code-style rules by ID and [options](../code-st
120120
> | [IDE0100](ide0100.md) | Remove unnecessary equality operator | |
121121
> | [IDE0110](ide0110.md) | Remove unnecessary discard | |
122122
> | [IDE0140](ide0140.md) | Simplify object creation | [visual_basic_style_prefer_simplified_object_creation](ide0140.md#visual_basic_style_prefer_simplified_object_creation) |
123+
> | [IDE0150](ide0150.md) | Prefer `null` check over type check | [csharp_style_prefer_null_check_over_type_check](ide0150.md#csharp_style_prefer_null_check_over_type_check) |
124+
> | [IDE0170](ide0170.md) | Simplify property pattern | [csharp_style_prefer_extended_property_pattern](ide0170.md#csharp_style_prefer_extended_property_pattern) |
125+
> | [IDE0180](ide0180.md) | Use tuple to swap values | [csharp_style_prefer_tuple_swap](ide0180.md#csharp_style_prefer_tuple_swap) |
123126
> | [IDE1005](ide1005.md) | Use conditional delegate call | [csharp_style_conditional_delegate_call](ide1005.md#csharp_style_conditional_delegate_call) |
124127
> | [IDE1006](naming-rules.md) | Naming styles | |
125128

0 commit comments

Comments
 (0)