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 should be disposed but the method or function is not annotated with [MustDisposeResource]

Last modified: 08 May 2024

If you are using the [MustDisposeResourceAttribute] from JetBrains.Annotations to enforce resource disposal in the calling code, and a method that obtains a resource from an annotated source does not handle resource disposal, but returns it to other callers, the problem of potential incorrect handling of the resource is passed to the callers. ReSharper reports such cases and suggests annotating the method with [MustDisposeResource] to explicitly delegate the responsibility of handling the disposable resource to the callers: