Skip to content

Commit

Permalink
Add documentation for CA2255 (new in .NET 6) (#27096)
Browse files Browse the repository at this point in the history
* Add documentation for CA2255 (new in .NET 6)

* Address pr feedback
  • Loading branch information
jeffhandley authored and IEvangelist committed Nov 18, 2021
1 parent 27f8137 commit 657431b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The following rules are enabled, by default, in .NET 6.
| [CA2200](quality-rules/ca2200.md) | Usage | Warning | Rethrow to preserve stack details |
| [CA2252](quality-rules/ca2252.md) | Usage | Error | Opt in to preview features |
| [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> |
| CA2255 | Usage | Warning | The `ModuleInitializer` attribute should not be used in libraries |
| [CA2255](quality-rules/ca2255.md) | Usage | Warning | The `ModuleInitializer` attribute should not be used in libraries |
| CA2256 | Usage | Warning | All members declared in parent interfaces must have an implementation in a `DynamicInterfaceCastableImplementation`-attributed interface |
| CA2257 | Usage | Warning | Members defined on an interface with the `DynamicInterfaceCastableImplementationAttribute` should be `static` |
| CA2258 | Usage | Warning | Providing a `DynamicInterfaceCastableImplementation` interface in Visual Basic is unsupported |
Expand Down
40 changes: 40 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca2255.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: "CA2255: The 'ModuleInitializer' attribute should not be used in libraries"
description: "Learn about code analysis rule CA2255: The 'ModuleInitializer' attribute should not be used in libraries"
ms.date: 11/15/2021
ms.topic: reference
f1_keywords:
- CA2255
- ModuleInitializerAttributeShouldNotBeUsedInLibraries
helpviewer_keywords:
- ModuleInitializerAttributeShouldNotBeUsedInLibraries
- CA2255
author: jeffhandley
---
# CA2255: The `ModuleInitializer` attribute should not be used in libraries

| | Value |
|-|-|
| **Rule ID** |CA2255|
| **Category** |[Usage](usage-warnings.md)|
| **Fix is breaking or non-breaking** |Non-breaking|

## Cause

Applying <xref:System.Runtime.CompilerServices.ModuleInitializerAttribute> to a method within a Class Library.

## Rule description

Module initializers are intended to be used by application code to ensure an application's components are initialized before the application code begins executing. If library code declares a method with the <xref:System.Runtime.CompilerServices.ModuleInitializerAttribute>, it can interfere with application initialization and also lead to limitations in that application's trimming abilities. Library code should therefore not utilize the <xref:System.Runtime.CompilerServices.ModuleInitializerAttribute> attribute.

## How to fix violations

Instead of using methods with the <xref:System.Runtime.CompilerServices.ModuleInitializerAttribute>, a library should expose methods that can be used to initialize any components within the library, and allow the application to invoke the method during application initialization.

## When to suppress warnings

It is safe to suppress warnings from this rule if a solution uses a Class Library for code factoring purposes, and the <xref:System.Runtime.CompilerServices.ModuleInitializerAttribute> method is not part of a shared or distributed library or package.

## See also

- [Usage warnings](usage-warnings.md)
1 change: 1 addition & 0 deletions docs/fundamentals/code-analysis/quality-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ The following table lists code quality analysis rules.
> | [CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has. |
> | [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. |
> | [CA2252: Opt in to preview features](ca2252.md) | Opt in to preview features before using preview APIs. |
> | [CA2255: The `ModuleInitializer` attribute should not be used in libraries](ca2255.md) | Module initializers are intended to be used by application code to ensure an application's components are initialized before the application code begins executing. |
> | [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. |
> | [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. |
> | [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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ Usage rules support proper usage of .NET.
|[CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has.|
|[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.|
|[CA2252: Opt in to preview features](ca2252.md)|Opt in to preview features before using preview APIs.|
|[CA2255: The `ModuleInitializer` attribute should not be used in libraries](ca2255.md) | Module initializers are intended to be used by application code to ensure an application's components are initialized before the application code begins executing. |
2 changes: 2 additions & 0 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,8 @@ items:
href: code-analysis/quality-rules/ca2251.md
- name: CA2252
href: code-analysis/quality-rules/ca2252.md
- name: CA2255
href: code-analysis/quality-rules/ca2255.md
- name: Code style rules
items:
- name: Overview
Expand Down

0 comments on commit 657431b

Please sign in to comment.