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

Search Results for

    SCA1030: By-value Read From a Stored Container

    Do not read a non-copyable value by value from a stored container.

    Example

    LockGuard first = guards[0];                   // SCA1030: a copy out of the list
    CollectionsMarshal.AsSpan(guards)[0].Dispose(); // OK: in place
    if (byKey.Remove(key, out var taken)) taken.Dispose(); // OK: taken out
    

    How to Fix

    • Use a reference, such as a span over the elements of a list. See Lists, Dictionaries and Spans.
    • Take the value out with a member that removes it, such as Remove, Pop or TryDequeue.
    • Read from a new value, such as a task that a call has just returned, or a list or a task that this method created and has not passed on, at its last use. See Locals That the Method Owns.
    • Move the container first, where it is the method's own.

    Details

    A member on the symbol list, an await or a foreach hands out a non-copyable value by value from a stored container, which still holds the original. Or it hands one out from a view, which never owns its elements.

    • Where the container is a local that is not the method's own, the message says why. An example is 'list' is not the method's own anywhere in the method, because on line N a lambda or a local function uses it.
    • A constructor that creates a non-copyable value over a stored container, such as new ValueTask<LockGuard>(_task), hands out no copy by itself. Its message is worded for that: the new value reads what the stored one still holds.

    See Also

    These articles explain the rule behind this warning:

    • What a Member on the Symbol List Hands Out - reads from stored containers.
    • Last Use of a Variable - where a container local is new.
    • Tasks and Async Code - awaiting stored tasks.

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

    © Singulink. All rights reserved.