SCA1014: Async or Iterator Member of a Non-copyable Struct
Do not make an instance member of a non-copyable struct async or an iterator.
Example
// In a [NonCopyable] struct:
public async ValueTask DisposeAsync() { await ReleaseAsync(_lock); _lock = null; } // SCA1014: clears a copy
public ValueTask DisposeAsync() { var owner = _lock; _lock = null; return ReleaseAsync(owner); } // OK
How to Fix
- Do the work in a member that is neither
asyncnor an iterator, which can change the struct and return the task of a static method. - Or do it in a static method that is given what it needs, such as a static iterator over an array of the struct.
Details
A non-copyable struct has an instance member that is async or an iterator, including an accessor. The compiler copies this into the state machine of such a member, so it runs on a copy of the struct, and what it changes is lost.
It also runs later, on a copy that is out of date by then, which is why a readonly member gets a warning as well. A readonly struct gets none: no copy of it can differ from the original.
See Also
These articles explain the rule behind this warning:
- Fields, Interfaces and Async Members - the declarations that copy a value by themselves.