diff --git a/docs/fundamentals/code-analysis/code-quality-rule-options.md b/docs/fundamentals/code-analysis/code-quality-rule-options.md index 12c8f7b3d4194..94985a7ff2680 100644 --- a/docs/fundamentals/code-analysis/code-quality-rule-options.md +++ b/docs/fundamentals/code-analysis/code-quality-rule-options.md @@ -135,7 +135,7 @@ This section lists the available configuration options for code analyzers. For m | Description | Allowable values | Default value | Configurable rules | |-------------|------------------|---------------|--------------------| -| Specifies that code in a project that generates this type of assembly should be analyzed | One or more fields of the enumeration

Separate multiple values with a comma (,) | All output kinds | [CA1515](quality-rules/ca1515.md) [CA2007](quality-rules/ca2007.md) | +| Specifies that code in a project that generates this type of assembly should be analyzed | One or more fields of the enumeration

Separate multiple values with a comma (,) | All output kinds | [CA1515](quality-rules/ca1515.md), [CA1516](quality-rules/ca1516.md), [CA2007](quality-rules/ca2007.md) | ### required_modifiers diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1516.md b/docs/fundamentals/code-analysis/quality-rules/ca1516.md new file mode 100644 index 0000000000000..da17eb0255453 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/ca1516.md @@ -0,0 +1,104 @@ +--- +title: "CA1516: Use cross-platform intrinsics" +description: "Learn about code analyzer rule CA1516 - Use cross-platform intrinsics" +ms.date: 07/09/2025 +ms.topic: reference +f1_keywords: + - CA1516 + - UseCrossPlatformIntrinsicsAnalyzer +helpviewer_keywords: + - CA1516 +author: TannerGooding +dev_langs: + - CSharp +--- + +# CA1516: Use cross-platform intrinsics + +| Property | Value | +|-------------------------------------|------------------------------------------------| +| **Rule ID** | CA1516 | +| **Title** | Use cross-platform intrinsics | +| **Category** | [Maintainability](maintainability-warnings.md) | +| **Fix is breaking or non-breaking** | Non-breaking | +| **Enabled by default in .NET 9** | No | + +## Cause + +A platform or architecture specific intrinsic is used when a cross-platform equivalent exists. + +## Rule description + +This rule detects usage of platform-specific intrinsics that can be replaced with an equivalent cross-platform intrinsic instead. + +## How to fix violations + +Apply the fixer that switches the code to use the equivalent cross-platform intrinsic. + +## Example + +The following code snippet shows three similar violations of CA1516: + +```csharp +using System; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; +using System.Runtime.Intrinsics.Wasm; +using System.Runtime.Intrinsics.X86; + +class C +{ + Vector128 M1(Vector128 x, Vector128 y) => AdvSimd.Add(x, y); + Vector128 M2(Vector128 x, Vector128 y) => Sse2.Add(x, y); + Vector128 M3(Vector128 x, Vector128 y) => PackedSimd.Add(x, y); +} +``` + +The following code snippet fixes the violation and would be applied by the fixer: + +```csharp +using System; +using System.Runtime.Intrinsics; + +class C +{ + Vector128 M1(Vector128 x, Vector128 y) => x + y; + Vector128 M2(Vector128 x, Vector128 y) => x + y; + Vector128 M3(Vector128 x, Vector128 y) => x + y; +} +``` + +Once the fix has been applied, it becomes more obvious that the three methods could be simplified to be a single method that works on all platforms. + +## When to suppress warnings + +It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code. + +## Suppress a warning + +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. + +```csharp +#pragma warning disable CA1516 +// The code that's violating the rule is on this line. +#pragma warning restore CA1516 +``` + +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). + +```ini +[*.{cs,vb}] +dotnet_diagnostic.CA1516.severity = none +``` + +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). + +## Configure code to analyze + +You can configure which _output assembly kinds_ to apply this rule to. For example, to only apply this rule to code that produces a console application or a dynamically linked library (that is, not a UI app), add the following key-value pair to an *.editorconfig* file in your project: + +```ini +dotnet_code_quality.CA1516.output_kind = ConsoleApplication, DynamicallyLinkedLibrary +``` + +For more information, see [output_kind](../code-quality-rule-options.md#output_kind). diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 3a5f7c27fc3b8..402e709f700d4 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -94,6 +94,7 @@ The following table lists code quality analysis rules. > | [CA1513: Use ObjectDisposedException throw helper](ca1513.md) | Throw helpers are simpler and more efficient than `if` blocks that construct a new exception instance. | > | [CA1514: Avoid redundant length argument](ca1514.md) | A redundant length argument is used when slicing to the end of a string or buffer. A calculated length can be error-prone and is also unnecessary. | > | [CA1515: Consider making public types internal](ca1515.md) | Unlike a class library, an application's API isn't typically referenced publicly, so types can be marked internal. | +> | [CA1516: Use cross-platform intrinsics](ca1516.md) | This rule detects usage of platform-specific intrinsics that can be replaced with an equivalent cross-platform intrinsic instead. | > | [CA1700: Do not name enum values 'Reserved'](ca1700.md) | This rule assumes that an enumeration member that has a name that contains "reserved" is not currently used but is a placeholder to be renamed or removed in a future version. Renaming or removing a member is a breaking change. | > | [CA1707: Identifiers should not contain underscores](ca1707.md) | By convention, identifier names do not contain the underscore (_) character. This rule checks namespaces, types, members, and parameters. | > | [CA1708: Identifiers should differ by more than case](ca1708.md) | Identifiers for namespaces, types, members, and parameters cannot differ only by case because languages that target the common language runtime are not required to be case-sensitive. | diff --git a/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md b/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md index 198dd8358e457..5da85471d19d2 100644 --- a/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/maintainability-warnings.md @@ -32,6 +32,7 @@ Maintainability rules support library and application maintenance. | [CA1513: Use ObjectDisposedException throw helper](ca1513.md) | Throw helpers are simpler and more efficient than `if` blocks that construct a new exception instance. | | [CA1514: Avoid redundant length argument](ca1514.md) | A redundant length argument is used when slicing to the end of a string or buffer. A calculated length can be error-prone and is also unnecessary. | | [CA1515: Consider making public types internal](ca1515.md) | Unlike a class library, an application's API isn't typically referenced publicly, so types can be marked internal. | +| [CA1516: Use cross-platform intrinsics](ca1516.md) | This rule detects usage of platform-specific intrinsics that can be replaced with an equivalent cross-platform intrinsic instead. | ## See also diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index e78549125f464..ab7f9f1a28f19 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -951,6 +951,8 @@ items: href: ../../fundamentals/code-analysis/quality-rules/ca1514.md - name: CA1515 href: ../../fundamentals/code-analysis/quality-rules/ca1515.md + - name: CA1516 + href: ../../fundamentals/code-analysis/quality-rules/ca1516.md - name: Naming rules items: - name: Overview