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

Search Results for

    SCA1043: Type That Does Not Stay Erasable

    Keep a type that derives from an erasable type erasable.

    Example

    class Bag<[NonCopyable] T> { }
    
    class SortedBag<[NonCopyable] T> : Bag<T>, IProducer<T> { }                   // SCA1043: with a list of guards as T, the object
                                                                                  // could be converted on to IProducer<object>
    
    class GuardBag<[NonCopyable] T> : Bag<T>, IProducer<T> where T : struct { }   // OK: variance needs a reference type
    

    How to Fix

    • Mark the type parameter of the base type that receives the non-copyable type.
    • Constrain the type parameter to struct, where variance is the way out.
    • Do not derive from the erasable type.

    Details

    A type derives from an erasable type, or implements one, and adds a way around it:

    • a base class or an interface with bodies that are not checked for the non-copyable type that it receives;
    • an interface that has a reference type which mentions the non-copyable type in a position that is covariant in effect (out, or in inside in).

    A conversion sees only the type that it is written with, and the object behind it can be of a type that hands out more. So the analyzer checks such a type where it is declared, for every way in which it can be constructed.

    See Also

    These articles explain the rule behind this warning:

    • Variance and Erasable Types - conversions that variance makes, and erasable types.
    © Singulink. All rights reserved.