dotnet / roslyn-analyzers

MIT License
1.58k stars 465 forks source link

BannedApiAnalyzer Dispose should ban using as well #7251

Open Suchiman opened 6 months ago

Suchiman commented 6 months ago

Analyzer

Diagnostic ID: RS0030

Describe the improvement

When you ban the Dispose method like so:

M:System.Net.Http.HttpMessageInvoker.Dispose; Disposing HttpClient leads to socket exhaustion. Don't.

this should not just prevent calling .Dispose(); but also using (httpClient) as well as using var client = new HttpClient()

Describe suggestions on how to achieve the rule

Expand the existing analysis to consider using in the analysis as an call to Dispose and await using to DisposeAsync

AlmightyLks commented 6 months ago

Building up on this: In fact, I was able to write a small POC, allowing 4 out of 4 cases to detect usings in "Dispose" bans and 3 out of 4 cases to detect usings in "DisposeAsync" cases

Would really love to see this aspect of Dispose banning being covered