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

Search Results for

    SCA1023: Container Used as a Type That Hides Its Values

    Do not use a container of non-copyable values as a type that hides them.

    Example

    // list is a List<LockGuard>
    object boxed = list;                           // SCA1023
    IReadOnlyList<LockGuard> typed = list;         // OK
    

    How to Fix

    • Keep the non-copyable type visible, as IReadOnlyList<LockGuard> does.
    • Turn the rule down where containers reach object on purpose, through logging and assertions: it has an ID of its own for that. See Turning Rules Down.

    Details

    A container is used as a type that hides the non-copyable type: object, dynamic, a base class, an interface that is not a collection, or a type parameter that is not marked. As for SCA1022, variance, an override, a method group and a foreach can do that with no conversion written in the code, and so can a ??, a deconstruction and a spread.

    The argument of a member on the symbol list gets the warning unless the line marks the member borrows. A line with no marker says how the member treats the values that it holds, not that it forgets a reference that it is given: List<object>.Add keeps the container, and Monitor.Enter, which lock calls, keeps nothing.

    Other Cases Under This ID

    • A value of a marked type parameter that is converted to such a type, and a container that is bound to a marked type parameter whose constraint is such a type. See Containers Bound to a Type Parameter.
    • Boxing a generic struct that is non-copyable only through its type arguments, such as a tuple: see Boxes and Unions.
    • A member that clones its object, such as CultureInfo.Clone called on a container that derives from CultureInfo through a generic whose type parameter is not marked. A clones line names it, so no line of a symbol list file can vouch for it, and the message says so. See Clones. Where no declaration of the project judged an object's binding to such a generic, the object gets SCA1009 instead, for this use and every other use as a type that hides it.

    Lambdas With an Expression Body

    A lambda with an expression body returns the result of the call in its body, wanted or not:

    // Here myLock.EnterValueAsync() returns a ValueTask<LockGuard>, a struct that a box would copy
    Func<object> run = () => myLock.EnterValueAsync();     // SCA1023: boxes a ValueTask<LockGuard> that the lambda perhaps never meant to return
    Action start = () => { myLock.EnterValueAsync(); };   // OK: a statement body, with a delegate type that returns nothing
    

    The message says so. If the result is not wanted, give the lambda a statement body.

    What the Message Names

    When the messages of this rule and of SCA1022, SCA1031, SCA1032 and SCA1033 name a class that is a container only through what it derives from or implements, they name that type with it. An example is 'Waiters' (a container through 'IEnumerable<Task<LockGuard>>').

    See Also

    These articles explain the rule behind this warning:

    • Containers and Conversions - conversions, also those with nothing written.
    • Variance and Erasable Types - conversions that variance makes, and erasable types.
    • Marked Type Parameters - containers bound to a marked type parameter.

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

    © Singulink. All rights reserved.