SCA1021: Member That Requires a Copyable Type
Do not use a member that requires a copyable type with a non-copyable type.
Example
public sealed class Pool<[NonCopyable] T> where T : struct
{
[RequiresCopyable]
public T[] ToArray() => /* ... */;
}
LockGuard[] all = pool.ToArray(); // SCA1021: ToArray requires a copyable type
int[] numbers = new Pool<int>().ToArray(); // OK
How to Fix
Use a member that does not copy, such as one that hands out a reference or moves the value out.
Details
A member marked [RequiresCopyable] is used with a non-copyable type. That includes:
- a generic type whose parameterless constructor is marked, used with a non-copyable type argument and bound to a type parameter that has the
new()constraint; - a constructor of a class that implicitly calls a marked constructor of its base class, which gets the warning where it is declared, or at the name of a class that has no constructor;
- a
withon a record, which uses the copy constructor of the record and of its base records.
See Members That Require a Copyable Type.
See Also
These articles explain the rule behind this warning:
- Marking a Type Parameter - generics that do not copy, and generics that are not marked.
- Marked Type Parameters - the exact rules.
Didn't add this analyzer yourself? See Why Am I Seeing This?