dotnet / roslyn-analyzers

MIT License
1.58k stars 464 forks source link

CA2016 - Flag cases where called methods accept a CancellationToken #7325

Open MattKotsenas opened 4 months ago

MattKotsenas commented 4 months ago

Analyzer

Diagnostic ID: CA2016

Describe the improvement

Currently rule CA2016 appears to be limited to cases where the containing method has a CancellationToken as a parameter. However, there are cases where a CancellationToken is available some other way (i.e. code analyzer's AnalysisContext).

It's also a signal to refactor when code is calling methods that take CancellationTokens but no token is available.

Describe suggestions on how to achieve the rule

Here's a good blog post that shows additional cases that can be caught: https://www.meziantou.net/detect-missing-cancellationtoken-using-a-roslyn-analyzer.htm

Additional context

I'm happy to take a first pass at this, but wanted to make sure this wasn't already considered and rejected.

sharwell commented 3 months ago

Related issues in the past:

https://github.com/DotNetAnalyzers/AsyncUsageAnalyzers/issues/36 https://github.com/DotNetAnalyzers/AsyncUsageAnalyzers/issues/37 https://github.com/DotNetAnalyzers/AsyncUsageAnalyzers/issues/43