-
Notifications
You must be signed in to change notification settings - Fork 6k
Docs for CA2252 #26149
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
Docs for CA2252 #26149
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||||
--- | ||||||||
title: "CA2252: Opt in to preview features" | ||||||||
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." | ||||||||
ms.date: 09/14/2021 | ||||||||
ms.topic: reference | ||||||||
f1_keywords: | ||||||||
- CA2252 | ||||||||
- DetectPreviewFeaturesAnalyzer | ||||||||
helpviewer_keywords: | ||||||||
- DetectPreviewFeaturesAnalyzer | ||||||||
- CA2252 | ||||||||
author: pgovind | ||||||||
ms.author: prgovi | ||||||||
dev_langs: | ||||||||
- CSharp | ||||||||
--- | ||||||||
# CA2252: Opt in to preview features before using them | ||||||||
|
||||||||
| | Value | | ||||||||
|-|-| | ||||||||
| **Rule ID** |CA2252| | ||||||||
| **Category** |[Microsoft.Usage](usage-warnings.md)| | ||||||||
| **Fix is breaking or non-breaking** |Non-breaking| | ||||||||
|
||||||||
## Cause | ||||||||
|
||||||||
A client uses preview APIs or types in their assembly without explicitly opting in either locally or at the module or assembly level. | ||||||||
|
||||||||
## Rule description | ||||||||
|
||||||||
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: | ||||||||
|
||||||||
- It is within the scope of a `RequiresPreviewFeaturesAttribute` annotation. | ||||||||
- It is part of an assembly or module that has already opted in to preview features. | ||||||||
|
||||||||
The following image shows an example of the CA2252 diagnostic. | ||||||||
|
||||||||
>  | ||||||||
|
||||||||
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`. | ||||||||
|
||||||||
## How to fix violations | ||||||||
|
||||||||
There are two ways to fix violations: | ||||||||
|
||||||||
- 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. | ||||||||
|
||||||||
- 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: | ||||||||
|
||||||||
```csharp | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
<PropertyGroup> | ||||||||
<EnablePreviewFeatures>true</EnablePreviewFeatures> | ||||||||
</PropertyGroup> | ||||||||
``` | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you actually mean to delete the ``` here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this must be a GitHub bug. The actual suggestion is to indent the ``` by two spaces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gewarren Using backticks inside backticks don't always work well. Whenever I do suggestions involving backticks I use:
instead of
The following is using
Suggested change
Just in case you come across this in future, it has bothered me for long before I knew about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's awesome, thanks @Youssef1313 ! |
||||||||
|
||||||||
## When to suppress errors | ||||||||
|
||||||||
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`. | ||||||||
The following images show how to disable the CA2252 analyzer locally. | ||||||||
|
||||||||
>  | ||||||||
|
||||||||
>  | ||||||||
|
||||||||
pgovind marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
## See also | ||||||||
|
||||||||
- [EnablePreviewFeatures and GenerateRequiresPreviewFeaturesAttribute](../../../core/project-sdk/msbuild-props.md#enablepreviewfeatures) | ||||||||
- [Preview feature design document](https://github.com/dotnet/designs/blob/main/accepted/2021/preview-features/preview-features.md) |
Uh oh!
There was an error while loading. Please reload this page.