Skip to content
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

Update CA rules for .NET 8 #38045

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions docs/core/project-sdk/msbuild-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ The following MSBuild properties are documented in this section:

### AnalysisLevel

The `AnalysisLevel` property lets you specify a set of code analyzers to run according to a .NET release. Each .NET release, starting in .NET 5, has a set of code analysis rules. Of that set, the rules that are enabled by default for that release will analyze your code. For example, if you upgrade to .NET 7 but don't want the default set of code analysis rules to change, set `AnalysisLevel` to `6`.
The `AnalysisLevel` property lets you specify a set of code analyzers to run according to a .NET release. Each .NET release, starting in .NET 5, has a set of code analysis rules. Of that set, the rules that are enabled by default for that release will analyze your code. For example, if you upgrade to .NET 8 but don't want the default set of code analysis rules to change, set `AnalysisLevel` to `7`.

```xml
<PropertyGroup>
Expand All @@ -767,9 +767,6 @@ Optionally, starting in .NET 6, you can specify a compound value for this proper
</PropertyGroup>
```

> [!NOTE]
> If you set `AnalysisLevel` to `5-<mode>` or `5.0-<mode>` and then install the .NET 6 SDK and recompile your project, you may see unexpected new build warnings. For more information, see [dotnet/roslyn-analyzers#5679](https://github.com/dotnet/roslyn-analyzers/issues/5679).

Default value:

- If your project targets .NET 5 or later, or if you've added the [AnalysisMode](#analysismode) property, the default value is `latest`.
Expand All @@ -783,6 +780,10 @@ The following table shows the values you can specify.
| `latest-<mode>` | The latest code analyzers that have been released are used. The `<mode>` value determines which rules are enabled. |
| `preview` | The latest code analyzers are used, even if they are in preview. |
| `preview-<mode>` | The latest code analyzers are used, even if they are in preview. The `<mode>` value determines which rules are enabled. |
| `8.0` | The set of rules that was available for the .NET 8 release is used, even if newer rules are available. |
| `8.0-<mode>` | The set of rules that was available for the .NET 8 release is used, even if newer rules are available. The `<mode>` value determines which rules are enabled. |
| `8` | The set of rules that was available for the .NET 8 release is used, even if newer rules are available. |
| `8-<mode>` | The set of rules that was available for the .NET 8 release is used, even if newer rules are available. The `<mode>` value determines which rules are enabled. |
| `7.0` | The set of rules that was available for the .NET 7 release is used, even if newer rules are available. |
| `7.0-<mode>` | The set of rules that was available for the .NET 7 release is used, even if newer rules are available. The `<mode>` value determines which rules are enabled. |
| `7` | The set of rules that was available for the .NET 7 release is used, even if newer rules are available. |
Expand All @@ -798,13 +799,13 @@ The following table shows the values you can specify.

> [!NOTE]
>
> - In .NET 5 and earlier versions, this property only affects [code-quality (CAXXXX) rules](../../fundamentals/code-analysis/quality-rules/index.md). Starting in .NET 6, if you set [EnforceCodeStyleInBuild](#enforcecodestyleinbuild) to `true`, this property affects [code-style (IDEXXXX) rules](../../fundamentals/code-analysis/style-rules/index.md) too.
> - Starting in .NET 6, if you set [EnforceCodeStyleInBuild](#enforcecodestyleinbuild) to `true`, this property affects [code-style (IDEXXXX) rules](../../fundamentals/code-analysis/style-rules/index.md) (in addition to code-quality rules).
> - If you set a compound value for `AnalysisLevel`, you don't need to specify an [AnalysisMode](#analysismode). However, if you do, `AnalysisLevel` takes precedence over `AnalysisMode`.
> - This property has no effect on code analysis in projects that don't reference a [project SDK](overview.md), for example, legacy .NET Framework projects that reference the Microsoft.CodeAnalysis.NetAnalyzers NuGet package.

### AnalysisLevel\<Category>

Introduced in .NET 6, this property is the same as [AnalysisLevel](#analysislevel), except that it only applies to a specific [category of code-analysis rules](../../fundamentals/code-analysis/categories.md). This property allows you to use a different version of code analyzers for a specific category, or to enable or disable rules at a different level to the other rule categories. If you omit this property for a particular category of rules, it defaults to the [AnalysisLevel](#analysislevel) value. The available values are the same as those for [AnalysisLevel](#analysislevel).
This property is the same as [AnalysisLevel](#analysislevel), except that it only applies to a specific [category of code-analysis rules](../../fundamentals/code-analysis/categories.md). This property allows you to use a different version of code analyzers for a specific category, or to enable or disable rules at a different level to the other rule categories. If you omit this property for a particular category of rules, it defaults to the [AnalysisLevel](#analysislevel) value. The available values are the same as those for [AnalysisLevel](#analysislevel).

```xml
<PropertyGroup>
Expand Down Expand Up @@ -837,34 +838,33 @@ The following table lists the property name for each rule category.

### AnalysisMode

Starting with .NET 5, the .NET SDK ships with all of the ["CA" code quality rules](../../fundamentals/code-analysis/quality-rules/index.md). By default, only [some rules are enabled](../../fundamentals/code-analysis/overview.md#enabled-rules) as build warnings in each .NET release. The `AnalysisMode` property lets you customize the set of rules that's enabled by default. You can either switch to a more aggressive analysis mode where you can opt out of rules individually, or a more conservative analysis mode where you can opt in to specific rules. For example, if you want to enable all rules as build warnings, set the value to `All`.
The .NET SDK ships with all of the ["CA" code quality rules](../../fundamentals/code-analysis/quality-rules/index.md). By default, only [some rules are enabled](../../fundamentals/code-analysis/overview.md#enabled-rules) as build warnings in each .NET release. The `AnalysisMode` property lets you customize the set of rules that's enabled by default. You can either switch to a more aggressive analysis mode where you can opt out of rules individually, or a more conservative analysis mode where you can opt in to specific rules. For example, if you want to enable all rules as build warnings, set the value to `All`.

```xml
<PropertyGroup>
<AnalysisMode>All</AnalysisMode>
</PropertyGroup>
```

The following table shows the available option values in .NET 5 and later versions. They're listed in increasing order of the number of rules they enable.
The following table shows the available option values. They're listed in increasing order of the number of rules they enable.

| .NET 6+ value | .NET 5 value | Meaning |
|-|-|-|
| `None` | `AllDisabledByDefault` | All rules are disabled. You can selectively [opt in to](../../fundamentals/code-analysis/configuration-options.md) individual rules to enable them. |
| `Default` | `Default` | Default mode, where certain rules are enabled as build warnings, certain rules are enabled as Visual Studio IDE suggestions, and the remainder are disabled. |
| `Minimum` | N/A | More aggressive mode than `Default` mode. Certain suggestions that are highly recommended for build enforcement are enabled as build warnings. To see which rules this includes, inspect the *%ProgramFiles%/dotnet/sdk/\[version]/Sdks/Microsoft.NET.Sdk/analyzers/build/config/analysislevel_\[level]_minimum.editorconfig* file. |
| `Recommended` | N/A | More aggressive mode than `Minimum` mode, where more rules are enabled as build warnings. To see which rules this includes, inspect the *%ProgramFiles%/dotnet/sdk/\[version]/Sdks/Microsoft.NET.Sdk/analyzers/build/config/analysislevel_\[level]_recommended.editorconfig* file. |
| `All` | `AllEnabledByDefault` | All rules are enabled as build warnings. You can selectively [opt out](../../fundamentals/code-analysis/configuration-options.md) of individual rules to disable them. |
| .NET 6+ value | Meaning |
|-|-|
| `None` | All rules are disabled. You can selectively [opt in to](../../fundamentals/code-analysis/configuration-options.md) individual rules to enable them. |
| `Default` | Default mode, where certain rules are enabled as build warnings, certain rules are enabled as Visual Studio IDE suggestions, and the remainder are disabled. |
| `Minimum` | More aggressive mode than `Default` mode. Certain suggestions that are highly recommended for build enforcement are enabled as build warnings. To see which rules this includes, inspect the *%ProgramFiles%/dotnet/sdk/\[version]/Sdks/Microsoft.NET.Sdk/analyzers/build/config/analysislevel_\[level]_minimum.editorconfig* file. |
| `Recommended` | More aggressive mode than `Minimum` mode, where more rules are enabled as build warnings. To see which rules this includes, inspect the *%ProgramFiles%/dotnet/sdk/\[version]/Sdks/Microsoft.NET.Sdk/analyzers/build/config/analysislevel_\[level]_recommended.editorconfig* file. |
| `All` | All rules are enabled as build warnings. You can selectively [opt out](../../fundamentals/code-analysis/configuration-options.md) of individual rules to disable them. |

> [!NOTE]
>
> - In .NET 5, this property only affects [code-quality (CAXXXX) rules](../../fundamentals/code-analysis/quality-rules/index.md). Starting in .NET 6, if you set [EnforceCodeStyleInBuild](#enforcecodestyleinbuild) to `true`, this property affects [code-style (IDEXXXX) rules](../../fundamentals/code-analysis/style-rules/index.md) too.
> - If you use a compound value for [AnalysisLevel](#analysislevel), for example, `<AnalysisLevel>5-recommended</AnalysisLevel>`, you can omit this property entirely. However, if you specify both properties, `AnalysisLevel` takes precedence over `AnalysisMode`.
> - If `AnalysisMode` is set to `AllEnabledByDefault` and `AnalysisLevel` is set to `5` or `5.0`, and then you install the .NET 6 SDK and recompile your project, you may see unexpected new build warnings. For more information, see [dotnet/roslyn-analyzers#5679](https://github.com/dotnet/roslyn-analyzers/issues/5679).
> - Starting in .NET 6, if you set [EnforceCodeStyleInBuild](#enforcecodestyleinbuild) to `true`, this property affects [code-style (IDEXXXX) rules](../../fundamentals/code-analysis/style-rules/index.md) (in addition to code-quality rules).
> - If you use a compound value for [AnalysisLevel](#analysislevel), for example, `<AnalysisLevel>8-recommended</AnalysisLevel>`, you can omit this property entirely. However, if you specify both properties, `AnalysisLevel` takes precedence over `AnalysisMode`.
> - This property has no effect on code analysis in projects that don't reference a [project SDK](overview.md), for example, legacy .NET Framework projects that reference the Microsoft.CodeAnalysis.NetAnalyzers NuGet package.

### AnalysisMode\<Category>

Introduced in .NET 6, this property is the same as [AnalysisMode](#analysismode), except that it only applies to a specific [category of code-analysis rules](../../fundamentals/code-analysis/categories.md). This property allows you to enable or disable rules at a different level to the other rule categories. If you omit this property for a particular category of rules, it defaults to the [AnalysisMode](#analysismode) value. The available values are the same as those for [AnalysisMode](#analysismode).
This property is the same as [AnalysisMode](#analysismode), except that it only applies to a specific [category of code-analysis rules](../../fundamentals/code-analysis/categories.md). This property allows you to enable or disable rules at a different level to the other rule categories. If you omit this property for a particular category of rules, it defaults to the [AnalysisMode](#analysismode) value. The available values are the same as those for [AnalysisMode](#analysismode).

```xml
<PropertyGroup>
Expand Down Expand Up @@ -924,9 +924,6 @@ The `CodeAnalysisTreatWarningsAsErrors` property lets you configure whether code

All code style rules that are [configured](../../fundamentals/code-analysis/overview.md#code-style-analysis) to be warnings or errors will execute on build and report violations.

> [!NOTE]
> If you install .NET 6 (or Visual Studio 2022, which includes .NET 6) but want to build your project using Visual Studio 2019, you might see new **CS8032** warnings if you have the `EnforceCodeStyleInBuild` property set to `true`. To resolve the warnings, you can specify the version of the .NET SDK to build your project with (in this case, something like `5.0.404`) by adding a [global.json entry](../tools/global-json.md).

### _SkipUpgradeNetAnalyzersNuGetWarning

The `_SkipUpgradeNetAnalyzersNuGetWarning` property lets you configure whether you receive a warning if you're using code analyzers from a NuGet package that's out-of-date when compared with the code analyzers in the latest .NET SDK. The warning looks similar to:
Expand Down
31 changes: 31 additions & 0 deletions docs/fundamentals/code-analysis/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,37 @@ If rule violations are found by an analyzer, they're reported as a suggestion, w

### Enabled rules

# [.NET 8](#tab/net-8)

The following rules are enabled, by default, in .NET 8.

| Diagnostic ID | Category | Severity | Description |
| - | - | - | - |
| [CA1416](quality-rules/ca1416.md) | Interoperability | Warning | Validate platform compatibility |
| [CA1417](quality-rules/ca1417.md) | Interoperability | Warning | Do not use `OutAttribute` on string parameters for P/Invokes |
| [CA1418](quality-rules/ca1418.md) | Interoperability | Warning | Use valid platform string |
| [CA1420](quality-rules/ca1420.md) | Interoperability | Warning | Using features that require runtime marshalling when it's disabled will result in run-time exceptions |
| [CA1422](quality-rules/ca1422.md) | Interoperability | Warning | Validate platform compatibility |
| [CA1831](quality-rules/ca1831.md) | Performance | Warning | Use `AsSpan` instead of range-based indexers for string when appropriate |
| CA1856 | Performance | Error | Incorrect usage of `ConstantExpected` attribute |
| CA1857 | Performance | Warning | A constant is expected for the parameter |
| [CA2013](quality-rules/ca2013.md) | Reliability | Warning | Do not use `ReferenceEquals` with value types |
| [CA2014](quality-rules/ca2014.md) | Reliability | Warning | Do not use `stackalloc` in loops |
| [CA2015](quality-rules/ca2015.md) | Reliability | Warning | Do not define finalizers for types derived from <xref:System.Buffers.MemoryManager%601> |
| [CA2017](quality-rules/ca2017.md) | Reliability | Warning | Parameter count mismatch |
| [CA2018](quality-rules/ca2018.md) | Reliability | Warning | The `count` argument to `Buffer.BlockCopy` should specify the number of bytes to copy |
| [CA2200](quality-rules/ca2200.md) | Usage | Warning | Rethrow to preserve stack details |
| CA2021 | Reliability | Warning | Do not call `Enumerable.Cast<T>` or `Enumerable.OfType<T>` with incompatible types |
| [CA2247](quality-rules/ca2247.md) | Usage | Warning | Argument passed to `TaskCompletionSource` constructor should be <xref:System.Threading.Tasks.TaskCreationOptions> enum instead of <xref:System.Threading.Tasks.TaskContinuationOptions> |
| [CA2252](quality-rules/ca2252.md) | Usage | Error | Opt in to preview features |
| [CA2255](quality-rules/ca2255.md) | Usage | Warning | The `ModuleInitializer` attribute should not be used in libraries |
| [CA2256](quality-rules/ca2256.md) | Usage | Warning | All members declared in parent interfaces must have an implementation in a `DynamicInterfaceCastableImplementation`-attributed interface |
| [CA2257](quality-rules/ca2257.md) | Usage | Warning | Members defined on an interface with the `DynamicInterfaceCastableImplementationAttribute` should be `static` |
| [CA2258](quality-rules/ca2258.md) | Usage | Warning | Providing a `DynamicInterfaceCastableImplementation` interface in Visual Basic is unsupported |
| [CA2259](quality-rules/ca2259.md) | Usage | Warning | `ThreadStatic` only affects static fields |
| [CA2260](quality-rules/ca2260.md) | Usage | Warning | Use correct type parameter |
| CA2261 | Usage | Warning | Do not use `ConfigureAwaitOptions.SuppressThrowing` with `Task<TResult>` |

# [.NET 7](#tab/net-7)

The following rules are enabled, by default, in .NET 7.
Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1000.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Do not declare static members on generic types |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 7** | No |
| **Enabled by default in .NET 8** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1001.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Types that own disposable fields should be disposable |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking - If the type is not visible outside the assembly.<br/><br/>Breaking - If the type is visible outside the assembly. |
| **Enabled by default in .NET 7** | No |
| **Enabled by default in .NET 8** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1002.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ms.author: gewarren
| **Title** | Do not expose generic lists |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 7** | No |
| **Enabled by default in .NET 8** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1003.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Use generic event handler instances |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 7** | No |
| **Enabled by default in .NET 8** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1005.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ms.author: gewarren
| **Title** | Avoid excessive parameters on generic types |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 7** | No |
| **Enabled by default in .NET 8** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1008.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Enums should have zero value |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking - When you're prompted to add a `None` value to a non-flag enumeration. Breaking - When you're prompted to rename or remove any enumeration values. |
| **Enabled by default in .NET 7** | No |
| **Enabled by default in .NET 8** | No |

## Cause

Expand Down
Loading
Loading