ReSharper 2024.1 Help

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

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:

[MustDisposeResource] public class HasNativeResources : IDisposable { private IDisposable _resource; public void Dispose() { _resource.Dispose(); } } public class Test { public Test() { var resource = new HasNativeResources(); } }
[MustDisposeResource] public class HasNativeResources : IDisposable { private IDisposable _resource; public void Dispose() { _resource.Dispose(); } } public class Test { public Test() { using var resource = new HasNativeResources(); } }
Last modified: 08 April 2024