SCA1003: Use Where the Value May Be Copied
Do not use a non-copyable value where it may be copied.
Example
Take(_guard); // SCA1003: passed by value
Take(NonCopyable.Move(ref _guard)); // OK
Look(in _guard); // OK: passed by reference
How to Fix
- Pass by reference (
ref,in,ref readonly) where the callee only uses the value. - Move the value where the callee should own it, or make the call the last use of a local.
- Use the value in place instead of putting it in a tuple, an array or an interpolated string.
The message names what receives the copy. See Fixing Common Warnings.
Details
Uses That May Copy
A stored value is used where it may be copied:
- passed by value, even as the receiver of an extension method that takes it by value;
- put in a tuple, an array or an interpolated string;
- captured as the receiver of a delegate;
- used in a
usingstatement or as the operand ofwith; - looked into through
?., or by a pattern with a member that is notreadonly, while it is in a nullable.
var pair = (_guard, 1); // SCA1003: the tuple takes a copy
var all = new[] { _guard }; // SCA1003: the array takes a copy
string text = $"{_guard}"; // SCA1003: the hole takes a copy
Take(_held = guard); // SCA1003: a read of '_held', which now holds the guard
string fresh = $"{myLock.Enter()}"; // OK: a new value moves into the hole
The result of an assignment is a read of its target, so it counts as a stored value wherever it is used in one of these ways. A hole of an interpolated string takes its value like a by-value parameter, so a new value, or a local at its last use, is moved into it.
An argument for an in or ref readonly parameter that is not a variable is a copy too: the compiler copies it into a temporary. See Arguments for In Parameters.
Operations the Analyzer Does Not Know Yet
Some operations have no rule of their own yet. A stored value that is used by value in one of them is treated as copied, and gets this warning with a message that names the kind of operation:
__refvalue(reference, LockGuard);- a pointer dereference, and an element through a pointer;
- the call of a function pointer;
- any kind of operation or of pattern that a later compiler adds, until the analyzer is taught it.
Used in place, such an operation gets no warning.
One such use is an await foreach whose enumerator returns, from MoveNextAsync or DisposeAsync, an awaitable of its own that is not a task of the framework and carries a non-copyable type or a container. The compiler does not give the analyzer the members that await it. Pass by reference, or move the value.
Values the Compiler Passes On Twice
These constructs hand one value to two places, so they get this warning even for a new value:
| Construct | The two places |
|---|---|
| Combined delegates | Every target |
A user-defined && |
operator false, and then on |
| A call with an interpolated-string handler argument | The method, and the constructor of the handler |
x[i] += ... on an indexer |
The getter and the setter |
See Copies the Compiler Makes.
Patterns That Use a Part and the Whole
A pattern that uses a part of a value, and also takes or uses the value around it, gets this warning too: see Members That Use a Part of the Value.
See Also
These articles explain the rule behind this warning:
- New and Stored Values in Detail - what may be copied and what may not.
- Read-only References and Temporary Copies - where the compiler makes a copy by itself.
- Copies the Compiler Makes - constructs that copy a value with no copy written.
Didn't add this analyzer yourself? See Why Am I Seeing This?