ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Return value of a method annotated with [MustDisposeResource] is never disposed

Last modified: 25 September 2024

If you are using the [MustDisposeResourceAttribute] from JetBrains.Annotations to enforce resource disposal in the calling code, ReSharper reports cases where there is a variable initialized with a constructor or a factory method returning a disposable resource, and that variable is not handled properly.

To avoid an incorrect handling of the disposable resource, ReSharper suggests converting variable declaration into a using declaration or a using block: