SCA1008: MemberwiseClone of a Class That Can Be Derived From
Do not call MemberwiseClone on a class that another assembly can derive from.
Example
public class Options
{
public Options Copy() => (Options)MemberwiseClone(); // SCA1008: another assembly can derive from Options
}
public sealed class SealedOptions
{
public SealedOptions Copy() => (SealedOptions)MemberwiseClone(); // OK: no derived class
}
public class Settings
{
private readonly string _name;
public Settings(string name) => _name = name;
protected Settings(Settings other) => _name = other._name; // OK: copies only what Settings declares
public virtual Settings Copy() => new(this);
}
public class LockedSettings : Settings
{
private LockGuard _guard;
public LockedSettings(string name, LockGuard guard) : base(name) => _guard = guard;
protected LockedSettings(LockedSettings other) : base(other) { } // the copy gets no guard: this class decides
public override Settings Copy() => new LockedSettings(this);
}
How to Fix
There are four ways out:
- Seal the class. A sealed class has no derived class. This is not open to an abstract class, nor to one that is meant to be derived from.
- Do not call MemberwiseClone. Copy the fields of the class in a constructor that takes the object to copy (see Copying in a Constructor).
- Suppress the warning. The suppression is then a rule for every class that derives from this one (see Suppressing).
- Ship a
clonesline and suppress the warning. In a library, this keeps the class open and cloneable, and a class that derives from it in another project gets a warning for a non-copyable value (see Shipping a Clones Line).
Details
MemberwiseClone() clones the object that it is called on, whatever its class. The class of the receiver has no field of a non-copyable type, but it can be derived from in another assembly, where a derived class can hold a non-copyable value that the clone would copy. So this warning can appear in a project that copies nothing.
What Can Be Derived From in Another Assembly
A class can be derived from in another assembly if it, or a class in the same compilation that derives from it, meets all three of these:
- it is not sealed;
- it can be reached from outside its assembly, together with every type that contains it;
- it has a constructor that a derived class there can call.
| Case | Counts? |
|---|---|
| A base class whose constructors are all internal, with a public class that derives from it | Yes, through that public class |
| A nested class that is protected | Only where the class that contains it can be derived from in another assembly |
An internal type or constructor in an assembly with InternalsVisibleTo |
Yes: it counts as reachable |
| A file-local class | Never |
Where a derived class in the same compilation has such a field, the call gets a warning as the definite copy that it is (SCA1005). This warning has an ID of its own so that you can turn it off and still keep those warnings.
Copying in a Constructor
Copy the fields of the class in a constructor that takes the object to copy, and let each derived class do the same for its own fields, calling that constructor of its base class. Each class then copies only what it declares. A derived class that holds a non-copyable value then decides for itself what its copy gets, in code that the analyzer checks.
Suppressing
A suppression is a rule for every class that derives from this one: none of them may hold a non-copyable value, since the clone would copy it. For a class that is meant to be extended and cloned with MemberwiseClone, this is the expected answer. Write the rule into the documentation of the class, where the authors of derived classes read it.
If none of a project's classes is meant to be derived from by a class that holds non-copyable values, the project can turn the rule down in .editorconfig:
[*.cs]
dotnet_diagnostic.SCA1008.severity = none
Shipping a Clones Line
The message gives the line for the class. Add it to the symbol list file that ships with the library, then suppress this warning:
# NonCopyableSymbols.MyLibrary.txt
T:MyLibrary.Options;clones=complete
M:MyLibrary.Options.Copy;clones
The member line answers the warning that the type line brings for Copy: see A Member That Clones Its Object. Write the type line as clones=complete once the clones lines name every member that clones the object or hands it on. Without it, a project that uses the library gets a warning for each member that no line names (see Clones).
// In a project that uses the library
sealed class PinnedOptions : MyLibrary.Options
{
private GCHandle _pinned; // SCA1009: Options clones its objects, and the handle with them
}
The rule for derived classes then needs no documentation of its own: the analyzer tells the author of each one. See Clones.
A Member That Clones Its Object
In a library, a member gets this warning where all of these hold:
- it clones its own object, and has no
clonesline of its own; - its class clones itself, and another project can derive from that class or bind a type to it;
- another project can call it: it is a public, protected or protected internal member.
A project that calls the member on an object whose class binds a non-copyable type cannot see the clone, and goes by the lines that the library ships. The warning finds the members that clone their object themselves, to help you complete those lines before you write clones=complete on the type line:
- a call of
MemberwiseCloneon its object; - a call of a member that clones, on its object or given its object;
- a call on its object through an interface that the class implements with a member that clones, as
((ICloneable)this).Clone().
The warning is not a proof that the lines are complete, and so it is not a reason by itself to write clones=complete. A private or internal member gets no warning, since a project that references the library cannot see it and a line for it would have no effect there. So a public member that clones its object through a private helper gets none either: the line for it is yours to add. Nor does a member that gives its object to a static helper that clones it, or one that hands its object to other code, such as one that stores it in another object that it returns.
The warning appears only where the project builds a library, a .dll; an application's own lines are for itself.
public class Note : CultureInfo
{
public Note() : base("en-US") { }
public Note Copy() => (Note)Clone(); // SCA1008: add 'M:MyLibrary.Note.Copy;clones' to the shipped file
}
The message also gives a type line with clones=complete, to write once the clones lines name every member that clones the object or hands it on. The line it gives is:
- the line of the member's class, where that class has a
clonesline in a symbol list file of the library's build or in the built-in framework symbol list; - otherwise, the line of the class that clones itself, where that class is the library's own.
Either line takes effect in a project that uses the library.
A call of MemberwiseClone in such a class also gets this warning as a call on an open class, as above.
See Also
These articles explain the rule behind this warning:
- SCA1005 - the definite copies of MemberwiseClone.
- SCA1009 - a base class of a library that was not built with the analyzers.
Didn't add this analyzer yourself? See Why Am I Seeing This?