SCA1024: Non-copyable Value Given to Unchecked Code
Do not give a non-copyable value other than by value to code that was not built with the analyzer.
Example
ThirdParty.Leases.Inspect(ref _lease); // SCA1024
ThirdParty.Leases.Fill(leases); // SCA1024: a List<Lease> given to unchecked code; 'leases' is no longer the method's own
ThirdParty.Leases.Take(NonCopyable.Move(ref _lease)); // OK: passed by value, so moved
_lease.Release(); // OK: a member of the non-copyable type itself
How to Fix
Build the assembly with the analyzers, where it is a project or a library of your own. Its members are then checked code, and need no entry.
Add the member to a symbol list file with
borrows, if it uses what it is given during the call and keeps nothing of it. The message gives the ID:# NonCopyableSymbols.txt: the member only reads the lease during the call M:ThirdParty.Leases.Inspect(ThirdParty.Lease@);borrowsPass the value by value, which moves it.
Avoid the use.
Details
A member of an assembly that was not built with the analyzer gets at a value of a non-copyable type other than by value. That is by ref, in or ref readonly, inside a container, inside a view or through a pointer. Nothing checks what the member does with the value, so it can copy it. A container that is passed to it is no longer the caller's own afterwards.
The member can get at the value as an argument, as the receiver of an extension, or as an operand of an operator or a conversion. It can also get at it through the Invoke of a delegate type that such an assembly declares.
Two kinds of member are judged otherwise:
- A member of the non-copyable type itself is trusted with its receiver and with values of that type.
- A member whose parameter mentions a type parameter goes by the rules of generic code (SCA1020) instead.
See Also
These articles explain the rule behind this warning:
- Code Not Built With the Analyzer - the marker of an analyzed build, and what the rules ask of unchecked code.
- Symbol List Format - the
borrowsandnewmarkers.
Didn't add this analyzer yourself? See Why Am I Seeing This?