dotnet / dotnet/sdk

CA2025 DoNotPassDisposablesIntoUnawaitedTasks: Let users specify Wait-like methods

Open
#51,761 0 comments 2 reactions 0 assignees View on GitHub
Area-Microsoft.CodeAnalysis.NetAnalyzers Area-NetSDK untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Is your feature request related to a problem? Please describe.

[CA2025](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2025) (Do not pass 'IDisposable' instances into unawaited tasks) checks whether a task is awaited by checking for presence of the `await` operator, the `Wait` method call and the `Result` property access:

https://github.com/dotnet/sdk/blob/dc026948482e5d42e34b9e565f113c9d814e180c/src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.CodeQuality.Analyzers/QualityGuidelines/DoNotPassDisposablesIntoUnawaitedTasks.cs#L188-L193

Notably, this list is missing `GetAwaiter().GetResult()`, `Unwrap().GetAwaiter().GetResult()` and all of the variations in popular libraries:
* `Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException` (there's also `WaitWithoutException` which calls `Wait` and swallows exceptions)
* `TaskExtensions.EnsureCompleted` in `Azure.Core.Pipeline.Policies`, `Elastic.Clients.Elasticsearch` etc.

### Describe the solution you'd like

1. Let the user list APIs which perform task waiting. For example:

```editorconfig
dotnet_code_quality.CA2025.task_wait_methods = M:Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(System.Threading.Tasks.Task),M:Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException``1(System.Threading.Tasks.Task``1)
```

2. Handle `GetAwaiter().GetResult()` specifically.

With these two additions implemented, it'll be easy to avoid false positives.

### Additional context

None.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.