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

Search Results for

    SCA1010: Field in a Copyable Struct or a Record

    Do not declare a field of a non-copyable type in a copyable struct or a record.

    Example

    struct Session(LockGuard guard) { public void Close() => guard.Dispose(); }   // SCA1010: 'guard' is a field of a copyable struct
    
    [NonCopyable]
    struct Session(LockGuard guard) { public void Close() => guard.Dispose(); }   // OK
    

    How to Fix

    • Mark the struct [NonCopyable] too, so that it is not copied either.
    • Make it a class that is not a record.

    Details

    A struct that is not itself non-copyable has a field of a non-copyable type, or a record has one. Copying the struct would copy the field, and a record copies its fields in with and in the members that the compiler writes for it.

    • A parameter of a copyable struct's primary constructor that the struct's members use is such a field as well.
    • A static field gets no warning: no copy of a struct or a record copies it.

    See Also

    These articles explain the rule behind this warning:

    • Fields, Interfaces and Async Members - the declarations that copy a value by themselves.

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

    © Singulink. All rights reserved.