SCA1015: Awaiter of a Non-copyable Type
Do not await with an awaiter of a non-copyable type.
Example
[NonCopyable]
public struct LeaseAwaiter : INotifyCompletion
{
public bool IsCompleted => false;
public void OnCompleted(Action continuation) { }
public void GetResult() { }
}
public struct LeaseAwaitable { public LeaseAwaiter GetAwaiter() => default; }
await new LeaseAwaitable(); // SCA1015: the awaiter is copied into the state machine
How to Fix
Make the awaiter a class, or a copyable struct that refers to the state. The awaiters of the framework hold a reference to their task and are fine.
Details
An await has an awaiter of a non-copyable type. When the await suspends, the compiler stores the awaiter in a field of the state machine, and calls OnCompleted or UnsafeOnCompleted on the local that it copied the awaiter from. It calls GetResult on the field when the method resumes. So what OnCompleted takes out of the awaiter is still in the copy that GetResult sees.
You get this warning whatever the awaited type is.
See Also
These articles explain the rule behind this warning:
- Copies the Compiler Makes - the awaiter, and other copies with nothing written.
Didn't add this analyzer yourself? See Why Am I Seeing This?