SCA1041: MovedFrom Used Wrongly
Use [MovedFrom] on a 'ref' parameter that the method resets.
Example
static void Replace([MovedFrom] ref LockGuard slot, ref LockGuard old)
{
old = slot;
slot = Create(); // SCA1041: if Create throws, 'old' and 'slot' hold the same guard
}
static void ReplaceSafely([MovedFrom] ref LockGuard slot, ref LockGuard old)
{
LockGuard fresh = Create(); // what can fail comes first
old = slot;
slot = fresh; // OK: the last use of 'fresh'
}
How to Fix
- Do what can fail first, before the read of the parameter, into a local.
- Reset the parameter on every path, right after the read.
- Check a reference that can be null before the read, with
NonCopyable.ThrowIfNullRef(in x), orin thisforthisin a struct, as the message says.
Details
Paths Through the Method
You get a warning for [MovedFrom] when:
- it is on something that is not a
refparameter; - the method can return or throw after it reads the parameter and before it assigns it;
- the method can read the parameter a second time before it assigns it;
- between the read and the assignment it runs something that can fail, or that reaches the parameter in another way: see Between the Read and the Reset;
- it makes the parameter refer to something else (
value = ref other;).
Passing the parameter on by reference counts as the assignment only to an out or a [MovedFrom] parameter, and only before the read. After the read it is a call like any other.
Null References
Where the operation that runs is a read or a write through a reference that can be a null reference, the message names the reference and says to check it first. The check can be:
NonCopyable.ThrowIfNullRef(in x), orin thisforthisin a struct;- a method whose parameter is marked
[NotNullRef], which counts only in an assembly that was built with the analyzer.
The message says the same for a value that is stored at the end of a try block and followed by such a read or write. See Null References.
Primary Constructors
[MovedFrom] on a parameter of a primary constructor gets a warning where it is declared: class Session([MovedFrom] ref LockGuard guard). The code of a primary constructor is the initializers of the type and the call of the base constructor, which have no order in which the parameter could be read and then assigned. Declare an ordinary constructor, or move the value out in the one initializer that takes it: public LockGuard Held = NonCopyable.Move(ref guard);.
Every [MovedFrom] parameter is checked. If the analyzer meets a member whose paths it does not follow, it warns about that parameter once, at the end of the build, instead of letting it pass unchecked. Declare the member as an ordinary method or constructor, as the message says.
What a Method Leaves Behind
You also get this warning where a method leaves something in a [MovedFrom] parameter that its caller cannot own:
- a container that something else holds, in a parameter of a container type;
- in a generic move, anything but
defaultor what the method read from its[MovedFrom]parameters, in what it returns or in what it leaves behind.
See Moving Containers and Generic Moves.
See Also
These articles explain the rule behind this warning:
- MovedFrom Mark - the exact rules.
- Null References - references that are known to refer to something, and the NotNullRef mark.