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

Search Results for

    SCA1046: NotNullRef Where It Does Not Count

    Put [NotNullRef] on a reference parameter of a method that is not virtual.

    Example

    static void Check([NotNullRef] LockGuard guard) { }       // SCA1046: a by-value parameter
    static void Check([NotNullRef] ref LockGuard guard) { }   // counts (and gets SCA1047 until verified)
    

    How to Fix

    Put the mark on a ref, in, ref readonly or out parameter of a method that is not virtual, or of a local function, where the call itself determines which code runs.

    Details

    [NotNullRef] is on a parameter where it does not count, and a call of a method with such a mark proves nothing:

    • on a by-value parameter;
    • on a parameter of a virtual or abstract member, an override, a member of an interface, a delegate, a lambda, an operator, a constructor or an accessor, or on the receiver of an extension block;
    • on a parameter of a method that is [Conditional], or of a partial method with no body, which the compiler can leave out of a build.

    See Also

    These articles explain the rule behind this warning:

    • Null References - references that are known to refer to something, and the NotNullRef mark.
    © Singulink. All rights reserved.