JetBrains / JetBrains.Annotations

Annotations to increase the accuracy of JetBrains ReSharper/Rider code inspections
https://www.jetbrains.com/help/resharper/Code_Analysis__Code_Annotations.html
MIT License
31 stars 18 forks source link

MustDisposeResource is not allowed on struct types #31

Closed tvardero closed 2 months ago

tvardero commented 3 months ago

As of version 2023.3.0, it is not allowed to annotate struct types with attribute MustDisposeResource.

My example would be this:

[MustDisposeResource]
public struct ResourceToken<TResource> : IDisposable
where TResource : ResourceBase
{
    private readonly ResourceManager _manager;
    private readonly object lockObject = new();
    private TResource _value;
    private bool _disposed;

    public ResourceToken(ResourceManager manager, TResource value) { ... }

    public readonly TResource Value { ... }

    public void Dispose()
    {
        if (_disposed) return;

        lock (lockObject)
        {
            if (_disposed) return;

            _manager.NotifyTokenDisposed(_value.InstanceId);

            _value = null!;
            _disposed = true;
        }
    }
}

This ResourceToken is used for reference tracking for loaded resources, so I could release resource when it is no longer in use by consumers. Token should be disposed, otherwise resource reference count will leak. It is struct, and the attribute is valid only on properties, classes and fields.

Please allow using this attribute on structs.