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

Add documentation for CA2255 (new in .NET 6) #27096

Merged
merged 2 commits into from
Nov 17, 2021
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
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