-
Notifications
You must be signed in to change notification settings - Fork 6k
CA2025 Analyzer documentation #46051
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
base: main
Are you sure you want to change the base?
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,83 @@ | ||||||
--- | ||||||
title: "CA2025: Do not pass 'IDisposable' instances into unawaited tasks" | ||||||
description: "Learn about code analysis rule CA2025 - Do not pass 'IDisposable' instances into unawaited tasks" | ||||||
ms.date: 05/08/2025 | ||||||
ms.topic: reference | ||||||
f1_keywords: | ||||||
- CA2025 | ||||||
- DoNotPassDisposablesIntoUnawaitedTasksAnalyzer | ||||||
helpviewer_keywords: | ||||||
- CA2025 | ||||||
author: steveberdy | ||||||
dev_langs: | ||||||
- CSharp | ||||||
- VB | ||||||
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
...since you don't have a VB example, this isn't necessary. |
||||||
--- | ||||||
|
||||||
# CA2025: Do not pass 'IDisposable' instances into unawaited tasks | ||||||
|
||||||
| Property | Value | | ||||||
|-------------------------------------|------------------------------------------------------| | ||||||
| **Rule ID** | CA2025 | | ||||||
| **Title** | Do not pass 'IDisposable' instances into unawaited tasks | | ||||||
| **Category** | [Reliability](reliability-warnings.md) | | ||||||
| **Fix is breaking or non-breaking** | Non-breaking | | ||||||
| **Enabled by default in .NET 10** | As warning | | ||||||
|
||||||
## Cause | ||||||
|
||||||
An `IDisposable` instance is passed into an unawaited task and potentially disposed before the task is finished using the instance. | ||||||
|
||||||
## Rule description | ||||||
|
||||||
Unawaited tasks that use `IDisposable` instances may use those instances long after they have been disposed. Ensure tasks using those instances are completed before the instances are disposed. | ||||||
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
|
||||||
|
||||||
## Examples | ||||||
|
||||||
The following code snippets (and their VB equivalents) are violations of CA2025: | ||||||
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
|
||||||
|
||||||
```csharp | ||||||
public Task DoSomethingAsync() | ||||||
{ | ||||||
// Using statements and using blocks can both be violations | ||||||
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
|
||||||
using (var disposable = new DisposableThing()) | ||||||
{ | ||||||
return DoSomethingInternalAsync(disposable); | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
```csharp | ||||||
public async Task DoThingsAsync() | ||||||
{ | ||||||
var disposable = new DisposableThing(); | ||||||
var task = DoSomethingInternalAsync(disposable); | ||||||
// More code 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.
Suggested change
|
||||||
dispose.Dispose(); | ||||||
// It's a violation if arguments are disposed before the task is awaited | ||||||
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
|
||||||
await task.ConfigureAwait(false); | ||||||
} | ||||||
``` | ||||||
|
||||||
## When to suppress warnings | ||||||
|
||||||
Suppress these warnings if you know tasks finish using `IDisposable` instances before they're disposed. | ||||||
|
||||||
## 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 CA2025 | ||||||
// The code that's violating the rule is on this line. | ||||||
#pragma warning restore CA2025 | ||||||
``` | ||||||
|
||||||
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.CA2025.severity = none | ||||||
``` | ||||||
|
||||||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add this to the TOC file on this line?