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

Search Results for

    SCA1040: Mark Not Kept Against a Base Member

    Keep a [NonCopyable] mark, do not add [RequiresCopyable], [Borrowed] or [Kept], and neither add nor drop [MovedFrom], against a base member or a delegate.

    In short: give an override, an implementation or a delegate the marks of the member that it stands for.

    Example

    interface IHolder<[NonCopyable] T> { void Hold(T value); }
    
    class Holder<T> : IHolder<T> { public void Hold(T value) { } }                       // SCA1040: 'T' must be marked too
    class MarkedHolder<[NonCopyable] T> : IHolder<T> { public void Hold(T value) { } }   // OK
    

    How to Fix

    Mark Rule
    [NonCopyable] on a type parameter Keep it
    [RequiresCopyable], [return: Borrowed], [Kept] Do not add it where the member does not have it
    [MovedFrom] Put it on exactly the same parameters

    Change the marks of what stands for the member, or put the mark on the base member or the delegate type as well.

    Details

    What stands for a member is a derived type, an override, an implementation, and a lambda or a method group that is converted to a delegate. You get this warning when:

    Mark Warning when
    [NonCopyable] on a type parameter The mark is not kept
    [RequiresCopyable] It is added where the member that is overridden or implemented does not have it
    [RequiresCopyable] on the copy constructor of a derived record It is added where the base record's is not
    [return: Borrowed], [Kept] It is added where the member that is overridden or implemented, or the delegate that a method is converted to, does not have it. For [Kept], a parameter whose type is a type parameter, marked or not, counts as having it
    [MovedFrom] It is added or dropped: it must be on exactly the parameters on which the member that it stands for has it
    [RequiresCopyable] on a static constructor or a finalizer Always: the runtime calls both, so no call site could get a warning. The body is checked as if the mark were not there

    Method Groups That Keep

    A method whose parameter has a type parameter as its type keeps what it is given. When such a method is converted to a delegate whose parameter does not keep, the conversion gets a warning, just as an added [Kept] would:

    public sealed class Registry<[NonCopyable] T>
    {
        private readonly List<T> _all = new();
        public void Add(T item) => _all.Add(item);
    }
    
    public delegate void GuardsSink(List<LockGuard> guards);
    public delegate void KeptSink([Kept] List<LockGuard> guards);
    
    var registry = new Registry<List<LockGuard>>();
    GuardsSink onCreated = registry.Add;           // SCA1040: 'Registry<T>.Add(T)' keeps what it is given
    KeptSink keeping = registry.Add;               // OK: the delegate says that it keeps
    

    A member or a delegate of an assembly that was not built with the analyzer has no mark that counts. So an override or a lambda that adds [MovedFrom] against it gets a warning even where that member carries the mark, and the message says that the mark does not count there. See Code Not Built With the Analyzer.

    See Also

    These articles explain the rule behind this warning:

    • Keeping Marks on Overrides and Delegates - every mark, with its rule.
    © Singulink. All rights reserved.