Skip to content

Commit 76e0ffd

Browse files
authored
Improvements for CA config options (#44647)
1 parent 8b35683 commit 76e0ffd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+537
-376
lines changed

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

Lines changed: 141 additions & 91 deletions
Large diffs are not rendered by default.

docs/fundamentals/code-analysis/includes/api-surface.md

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
### Include specific API surfaces
2+
3+
You can configure which parts of your codebase to run this rule on, based on their accessibility, by setting the [api_surface](../../code-quality-rule-options.md#api_surface) option. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an *.editorconfig* file in your project:
4+
5+
```ini
6+
dotnet_code_quality.CAXXXX.api_surface = private, internal
7+
```
8+
9+
> [!NOTE]
10+
> Replace the `XXXX` part of `CAXXXX` with the ID of the applicable rule.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
### Configure dispose ownership transfer
2+
3+
The [dispose_ownership_transfer_at_constructor](../../code-quality-rule-options.md#dispose_ownership_transfer_at_constructor) and [dispose_ownership_transfer_at_method_call](../../code-quality-rule-options.md#dispose_ownership_transfer_at_method_call) options configure the transfer of dispose ownership.
4+
5+
For example, to specify that the rule transfers dispose ownership for arguments passed to constructors, add the following key-value pair to an *.editorconfig* file in your project:
6+
7+
```ini
8+
dotnet_code_quality.CAXXXX.dispose_ownership_transfer_at_constructor = true
9+
```
10+
11+
> [!NOTE]
12+
> Replace the `XXXX` part of `CAXXXX` with the ID of the applicable rule.
13+
14+
#### dispose_ownership_transfer_at_constructor
15+
16+
Consider the following code example.
17+
18+
```csharp
19+
class A : IDisposable
20+
{
21+
public void Dispose() { }
22+
}
23+
24+
class Test
25+
{
26+
DisposableOwnerType M1()
27+
{
28+
return new DisposableOwnerType(new A());
29+
}
30+
}
31+
```
32+
33+
- If `dotnet_code_quality.dispose_ownership_transfer_at_constructor` is set to `true`, dispose ownership for the `new A()` allocation is transferred to the returned `DisposableOwnerType` instance.
34+
- If `dotnet_code_quality.dispose_ownership_transfer_at_constructor` is set to `false`, `Test.M1()` has the dispose ownership for `new A()`, and results in a `CA2000` violation for a dispose leak.
35+
36+
#### dispose_ownership_transfer_at_method_call
37+
38+
Consider the following code example.
39+
40+
```csharp
41+
class Test
42+
{
43+
void M1()
44+
{
45+
TransferDisposeOwnership(new A());
46+
}
47+
}
48+
```
49+
50+
- If `dotnet_code_quality.dispose_ownership_transfer_at_method_call` is set to `true`, dispose ownership for the `new A()` allocation is transferred to the `TransferDisposeOwnership` method.
51+
- If `dotnet_code_quality.dispose_ownership_transfer_at_method_call` is set to `false`, `Test.M1()` has the dispose ownership for `new A()`, and results in a `CA2000` violation for a dispose leak.

docs/fundamentals/code-analysis/includes/excluded-symbol-names.md renamed to docs/fundamentals/code-analysis/includes/config-options/excluded-symbol-names.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### Exclude specific symbols
22

3-
You can exclude specific symbols, such as types and methods, from analysis. For example, to specify that the rule should not run on any code within types named `MyType`, add the following key-value pair to an *.editorconfig* file in your project:
3+
You can exclude specific symbols, such as types and methods, from analysis by setting the [excluded_symbol_names](../../code-quality-rule-options.md#excluded_symbol_names) option. For example, to specify that the rule should not run on any code within types named `MyType`, add the following key-value pair to an *.editorconfig* file in your project:
44

55
```ini
66
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType
@@ -12,7 +12,7 @@ dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType
1212
Allowed symbol name formats in the option value (separated by `|`):
1313

1414
- Symbol name only (includes all symbols with the name, regardless of the containing type or namespace).
15-
- Fully qualified names in the symbol's [documentation ID format](../../../csharp/language-reference/xmldoc/index.md#id-strings). Each symbol name requires a symbol-kind prefix, such as `M:` for methods, `T:` for types, and `N:` for namespaces.
15+
- Fully qualified names in the symbol's [documentation ID format](../../../../csharp/language-reference/xmldoc/index.md#id-strings). Each symbol name requires a symbol-kind prefix, such as `M:` for methods, `T:` for types, and `N:` for namespaces.
1616
- `.ctor` for constructors and `.cctor` for static constructors.
1717

1818
Examples:

docs/fundamentals/code-analysis/includes/excluded-type-names-with-derived-types.md renamed to docs/fundamentals/code-analysis/includes/config-options/excluded-type-names-with-derived-types.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### Exclude specific types and their derived types
22

3-
You can exclude specific types and their derived types from analysis. For example, to specify that the rule should not run on any methods within types named `MyType` and their derived types, add the following key-value pair to an *.editorconfig* file in your project:
3+
You can exclude specific types and their derived types from analysis by setting the [excluded_type_names_with_derived_types](../../code-quality-rule-options.md#excluded_type_names_with_derived_types) option. For example, to specify that the rule should not run on any methods within types named `MyType` and their derived types, add the following key-value pair to an *.editorconfig* file in your project:
44

55
```ini
66
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType
@@ -12,12 +12,12 @@ dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType
1212
Allowed symbol name formats in the option value (separated by `|`):
1313

1414
- Type name only (includes all types with the name, regardless of the containing type or namespace).
15-
- Fully qualified names in the symbol's [documentation ID format](../../../csharp/language-reference/xmldoc/index.md#id-strings), with an optional `T:` prefix.
15+
- Fully qualified names in the symbol's [documentation ID format](../../../../csharp/language-reference/xmldoc/index.md#id-strings), with an optional `T:` prefix.
1616

1717
Examples:
1818

1919
<!-- markdownlint-disable MD056 -->
20-
| Option Value | Summary |
20+
| Option value | Summary |
2121
| --- | --- |
2222
|`dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType` | Matches all types named `MyType` and all of their derived types. |
2323
|`dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType1|MyType2` | Matches all types named either `MyType1` or `MyType2` and all of their derived types. |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### Ignore InternalsVisibleTo attribute
2+
3+
By default, this rule is disabled if the assembly being analyzed uses <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute> to expose its internal symbols. You can set the [ignore_internalsvisibleto](../../code-quality-rule-options.md#ignore_internalsvisibleto) option to change the configuration. To specify that the rule should run even if the assembly is marked with <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute>, add the following key-value pair to an *.editorconfig* file in your project:
4+
5+
```ini
6+
dotnet_code_quality.CAXXXX.ignore_internalsvisibleto = true
7+
```
8+
9+
> [!NOTE]
10+
> Replace the `XXXX` part of `CAXXXX` with the ID of the applicable rule.
11+
12+
This option is available starting in .NET 8.

docs/fundamentals/code-analysis/includes/ignore-ivt.md

Lines changed: 0 additions & 12 deletions
This file was deleted.

docs/fundamentals/code-analysis/quality-rules/ca1000.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Use the following option to configure which parts of your codebase to run this r
8787

8888
You can configure this option for just this rule, for all rules that it applies to, or for all rules in this category ([Design](design-warnings.md)) that it applies to. For more information, see [Code quality rule configuration options](../code-quality-rule-options.md).
8989

90-
[!INCLUDE[api-surface](../includes/api-surface.md)]
90+
[!INCLUDE[api-surface](../includes/config-options/api-surface.md)]
9191

9292
## Related rules
9393

docs/fundamentals/code-analysis/quality-rules/ca1001.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ Use the following options to configure which parts of your codebase to run this
7272

7373
These options can be configured for just this rule, for all rules it applies to, or for all rules in this category ([Design](design-warnings.md)) that it applies to. For more information, see [Code quality rule configuration options](../code-quality-rule-options.md).
7474

75-
[!INCLUDE[excluded-symbol-names](../includes/excluded-symbol-names.md)]
75+
[!INCLUDE[excluded-symbol-names](../includes/config-options/excluded-symbol-names.md)]
7676

77-
[!INCLUDE[excluded-type-names-with-derived-types](../includes/excluded-type-names-with-derived-types.md)]
77+
[!INCLUDE[excluded-type-names-with-derived-types](../includes/config-options/excluded-type-names-with-derived-types.md)]
7878

7979
## Example
8080

0 commit comments

Comments
 (0)