dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.96k stars 4.03k forks source link

IDisposable analyzer #61850

Closed TonyValenti closed 2 years ago

TonyValenti commented 2 years ago

Brief description: A class that implements IDisposable is special because it may represent a resource (potentially unmanaged) that should be freed. As C# is increasingly used to write low-level code, this continually becomes more and more important.

An analyzer should exist that will flag when classes that implement IDisposable are created that:

  1. Are not disposed of (either explicitly or via using)
  2. (if possible), do not escape the current method (either via return, out, or assignment to a property, or by being passed into a method that would cause it to escape).
  3. Offered a fixer that would change var x = new <IDisposable Type> to using var x = new <IDisposable Type>

Additionally, it would be nice if the Visual Studio editor provided some type of unique colorization for variables that are of types that implement IDisposable.

Youssef1313 commented 2 years ago

Have you seen CA2000: Dispose objects before losing scope?

Other IDisposable-related analyzers:

https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2213 https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1063 https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2215 https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1001

vatsalyaagrawal commented 2 years ago

Closing. Please reopen if you have more questions.