Skip to content

Commit 3004781

Browse files
author
Prashanth Govindarajan
authored
Docs for CA2252 (#26149)
1 parent fd46a63 commit 3004781

File tree

8 files changed

+74
-0
lines changed

8 files changed

+74
-0
lines changed

docs/core/project-sdk/msbuild-props.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ When a project contains this property set to `True`, the following assembly-leve
375375

376376
An analyzer warns if this attribute is present on dependencies for projects where `EnablePreviewFeatures` is not set to `True`.
377377

378+
Library authors who intend to ship preview assemblies should set this property to `True`. If an assembly needs to ship with a mixture of preview and non-preview APIs, see the [GenerateRequiresPreviewFeaturesAttribute](#generaterequirespreviewfeaturesattribute) section below.
379+
378380
### GenerateRequiresPreviewFeaturesAttribute
379381

380382
The `GenerateRequiresPreviewFeaturesAttribute` property is closely related to the [EnablePreviewFeatures](#enablepreviewfeatures) property. If your library uses preview features but you don't want the entire assembly to be marked with the <xref:System.Runtime.Versioning.RequiresPreviewFeaturesAttribute> attribute, which would require any consumers to [enable preview features](#enablepreviewfeatures), set this property to `False`.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: "CA2252: Opt in to preview features"
3+
description: "Learn about the code analyzer that scans for preview API usage and produces a build error if an assembly has not opted in to preview features."
4+
ms.date: 09/14/2021
5+
ms.topic: reference
6+
f1_keywords:
7+
- CA2252
8+
- DetectPreviewFeaturesAnalyzer
9+
helpviewer_keywords:
10+
- DetectPreviewFeaturesAnalyzer
11+
- CA2252
12+
author: pgovind
13+
ms.author: prgovi
14+
dev_langs:
15+
- CSharp
16+
---
17+
# CA2252: Opt in to preview features before using them
18+
19+
| | Value |
20+
|-|-|
21+
| **Rule ID** |CA2252|
22+
| **Category** |[Microsoft.Usage](usage-warnings.md)|
23+
| **Fix is breaking or non-breaking** |Non-breaking|
24+
25+
## Cause
26+
27+
A client uses preview APIs or types in their assembly without explicitly opting in either locally or at the module or assembly level.
28+
29+
## Rule description
30+
31+
When an API or assembly that's decorated with the <xref:System.Runtime.Versioning.RequiresPreviewFeaturesAttribute> attribute is consumed, this rule checks if the call site has opted in to preview features. A call site has opted in to preview features if one of the following applies:
32+
33+
- It is within the scope of a `RequiresPreviewFeaturesAttribute` annotation.
34+
- It is part of an assembly or module that has already opted in to preview features.
35+
36+
The following image shows an example of the CA2252 diagnostic.
37+
38+
> ![Code editor with CA2252 warning.](media/ca2252-basic.png)
39+
40+
Here, `Lib` is a preview type that's constructed in the `Main` method. `Main` itself is not annotated as a preview method, so diagnostics are produced on the two constructors calls inside `Main`.
41+
42+
## How to fix violations
43+
44+
There are two ways to fix violations:
45+
46+
- Bring a call site within the scope of an annotation by annotating its parent with `RequiresPreviewFeaturesAttribute`. In the previous example, `APreviewMethod` is annotated with the `RequiresPreviewFeatures` attribute, so the analyzer ignores preview type usage inside `APreviewMethod`. It follows that callers of `APreviewMethod` will have to perform a similar exercise.
47+
48+
- You can also opt in to preview features at an assembly or module level. This indicates to the analyzer that preview type usage in the assembly is desired and, as a consequence, no errors will be produced by this rule. This is the preferred way to consume preview dependencies. To enable preview features inside the entire assembly, set the [EnablePreviewFeatures](../../../core/project-sdk/msbuild-props.md#enablepreviewfeatures) property in a `.csproj` file:
49+
50+
```csharp
51+
<PropertyGroup>
52+
<EnablePreviewFeatures>true</EnablePreviewFeatures>
53+
</PropertyGroup>
54+
```
55+
56+
## When to suppress errors
57+
58+
Suppressing errors from this rule is only recommended for advanced use cases where diagnostics on APIs need to explicitly disabled. In this case, you must be willing to take on the responsibility of marking preview APIs appropriately. For example, consider a case where an existing type implements a new preview interface. Since the entire type cannot be marked as preview (for backwards compatibility), the diagnostic around the type definition can be disabled locally. Further, you need to mark the preview interface implementations as preview. Now, the existing type can be used as before, but calls to the new interface methods will get diagnostics. *System.Private.CoreLib.csproj* uses this technique to expose generic math features on numeric types such as `Int32`, `Double`, and `Decimal`.
59+
The following images show how to disable the CA2252 analyzer locally.
60+
61+
> ![CA2252 - Suppress Detect Preview Feature Diagnostic](media/ca2252-advanced-1.png)
62+
63+
> ![CA2252 - Mark Interface Implementations Explicitly](media/ca2252-advanced-2.png)
64+
65+
## See also
66+
67+
- [EnablePreviewFeatures and GenerateRequiresPreviewFeaturesAttribute](../../../core/project-sdk/msbuild-props.md#enablepreviewfeatures)
68+
- [Preview feature design document](https://github.com/dotnet/designs/blob/main/accepted/2021/preview-features/preview-features.md)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ The following table lists code quality analysis rules.
184184
> | [CA2249: Consider using String.Contains instead of String.IndexOf](ca2249.md) | Calls to `string.IndexOf` where the result is used to check for the presence/absence of a substring can be replaced by `string.Contains`. |
185185
> | [CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has. |
186186
> | [CA2251: Use `String.Equals` over `String.Compare`](ca2251.md) | It is both clearer and likely faster to use `String.Equals` instead of comparing the result of `String.Compare` to zero. |
187+
> | [CA2252: Opt in to preview features](ca2252.md) | Opt in to preview features before using preview APIs. |
187188
> | [CA2300: Do not use insecure deserializer BinaryFormatter](ca2300.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
188189
> | [CA2301: Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder](ca2301.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
189190
> | [CA2302: Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize](ca2302.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
36.7 KB
Loading
123 KB
Loading
185 KB
Loading

docs/fundamentals/code-analysis/quality-rules/usage-warnings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ Usage rules support proper usage of .NET.
5454
|[CA2249: Consider using String.Contains instead of String.IndexOf](ca2249.md)|Calls to `string.IndexOf` where the result is used to check for the presence or absence of a substring can be replaced by `string.Contains`.|
5555
|[CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has.|
5656
|[CA2251: Use `String.Equals` over `String.Compare`](ca2251.md)|It is both clearer and likely faster to use `String.Equals` instead of comparing the result of `String.Compare` to zero.|
57+
|[CA2252: Opt in to preview features](ca2252.md)|Opt in to preview features before using preview APIs.|

docs/fundamentals/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,8 @@ items:
12851285
href: code-analysis/quality-rules/ca2250.md
12861286
- name: CA2251
12871287
href: code-analysis/quality-rules/ca2251.md
1288+
- name: CA2252
1289+
href: code-analysis/quality-rules/ca2252.md
12881290
- name: Code style rules
12891291
items:
12901292
- name: Overview

0 commit comments

Comments
 (0)