SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
795 stars 228 forks source link

New Rule Idea: Asynchronous methods should take a CancellationToken #4449

Open costin-zaharia-sonarsource opened 3 years ago

costin-zaharia-sonarsource commented 3 years ago

Source: https://community.sonarsource.com/t/c-asynchronous-methods-should-take-a-cancellationtoken/42190 Microsoft docs: https://docs.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/task-based-asynchronous-pattern-tap#cancellation-optional

costin-zaharia-sonarsource commented 3 years ago

From docs:

For methods that cannot be canceled, do not provide overloads that accept a cancellation token; this helps indicate to the caller whether the target method is actually cancelable.

This rule has the potential to raise a lot of false positives.

We should consider a second rule which checks the async method invocations and, if there are overloads with CancellationToken, will suggest using them.

luizfbicalho commented 11 months ago

Any information on this rule?