Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

steveberdy
Copy link
Contributor

@steveberdy steveberdy commented May 8, 2025

@steveberdy steveberdy requested review from gewarren and a team as code owners May 8, 2025 04:33
@dotnetrepoman dotnetrepoman bot added this to the May 2025 milestone May 8, 2025
@dotnet-policy-service dotnet-policy-service bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels May 8, 2025
Copy link
Contributor

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?

author: steveberdy
dev_langs:
- CSharp
- VB
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- VB

...since you don't have a VB example, this isn't necessary.


## 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Unawaited tasks that use `IDisposable` instances might use those instances long after they've been disposed. Ensure tasks using those instances are completed before the instances are disposed.


## Examples

The following code snippets (and their VB equivalents) are violations of CA2025:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The following code snippets (and their VB equivalents) are violations of CA2025:
The following code snippets (and their Visual Basic equivalents) are violations of CA2025:

```csharp
public Task DoSomethingAsync()
{
// Using statements and using blocks can both be violations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Using statements and using blocks can both be violations
// Using statements and using blocks can both be violations.

var task = DoSomethingInternalAsync(disposable);
// More code here
dispose.Dispose();
// It's a violation if arguments are disposed before the task is awaited
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// It's a violation if arguments are disposed before the task is awaited
// It's a violation if arguments are disposed before the task is awaited.

{
var disposable = new DisposableThing();
var task = DoSomethingInternalAsync(disposable);
// More code here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// More code here
// More code here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants