Code Analysis Code Analysis
Code Analysis Code Analysis
DocFX + Singulink = ♥

Search Results for

    SCA1033: Container Moved Out of Something the Method Does Not Own

    Do not hand over a container that the method does not own.

    Example

    LockGuard b = await NonCopyable.Move(ref _cached);   // SCA1033 and SCA1030: whoever else has the task can await it too
    
    var pending = myLock.EnterAsync();
    LockGuard a = await NonCopyable.Move(ref pending);   // OK: the method's own
    

    How to Fix

    • Move a local that holds what the method created or got from a call.
    • Use the container in place, where it is a slot that generic code passed in.
    • Suppress the warning, where a container must be taken out of a field: nothing can show that it is unshared.

    Details

    A container is moved out of something that is not a local which the method owns, by passing it to a [MovedFrom] parameter, such as that of NonCopyable.Move. That is a field, an element, a parameter that is not [MovedFrom] itself, or a local that something else may hold. Clearing one variable says nothing about the other references to a container. What the move hands out is stored as well, so a read from it gets a warning too (SCA1030).

    Slots From Generic Code

    Generic code can pass a slot of its own to a [MovedFrom] parameter without knowing that the slot holds a shared task. So the parameter is not the method's own where the member that it overrides or implements, or the delegate type that it is converted to, declares it as a type parameter:

    delegate void MoveAction<[NonCopyable] T>([MovedFrom] ref T value);
    
    MoveAction<Task<LockGuard>> take = ([MovedFrom] ref Task<LockGuard> task) =>
    {
        var mine = NonCopyable.Move(ref task);   // SCA1033: the slot came from generic code
    };
    

    Where the analyzer cannot see the body, as for the Invoke of another delegate, the warning appears where that delegate is converted.

    See Also

    These articles explain the rule behind this warning:

    • Moving Containers and Generic Moves - moving containers, and container parameters.
    • Locals That the Method Owns - which locals a method owns.

    Didn't add this analyzer yourself? See Why Am I Seeing This?

    © Singulink. All rights reserved.