SCA1025: Unchecked Assembly That Uses a Non-copyable Type
Build an assembly that uses a non-copyable type with the analyzer.
Example
The warning has no place in the source. You get it once for each referenced assembly, with a message such as this one, for a plugin assembly that uses the Lease struct of a library:
warning SCA1025: Assembly 'Contoso.Plugins' was not built with the analyzer, and it references 'Contoso.Locks' and with it non-copyable
type 'Contoso.Locks.Lease': its code can copy a value of that type where it implements an interface, makes a delegate or reads an
object of a type of checked code, which no call shows. Build 'Contoso.Plugins' with the analyzer, or, if its code makes no copy of the
non-copyable values that it gets at, vouch for it with the line 'A:Contoso.Plugins;trusted' in a NonCopyableSymbols.txt file.
For one of the framework's non-copyable types, such as SpinLock, the message names the member of your code that lets the assembly get at a value:
warning SCA1025: Assembly 'Contoso.Plugins' was not built with the analyzer, and it references 'Contoso.Jobs', where 'Job.Lock' lets
other code get at a non-copyable value: its code can copy such a value where it implements an interface, makes a delegate or reads
an object of a type of checked code, which no call shows. ...
How to Fix
Build the assembly with the analyzer. Where it is a project in the solution or a library of your own, reference the analyzers package there, or let the library that declares the type pass the analyzer on.
Vouch for the assembly where it cannot be built with the analyzer, and its code makes no copy of the non-copyable values that it gets at, even behind a checked type. A line of a symbol list file names it by its simple name:
# NonCopyableSymbols.txt: the plugins only pass the leases on A:Contoso.Plugins;trustedThe line turns off this warning for that assembly. It also turns off SCA1009 on a field of a class of yours that derives from one of its classes. It changes nothing else: its members stay unchecked code for every other rule.
A warning with no place in the source can be suppressed only for the whole project, in .editorconfig or with NoWarn, which would also hide the warning for every assembly that is added later. The trusted line is for one assembly.
Details
An assembly that the project references was not built with the analyzer, and it can get at a non-copyable value. That is so where it references an assembly that declares a non-copyable type, or one whose members let other code get at such a value. The analyzer judges a call by the member that it binds to, so it does not see the code of such an assembly where that code runs behind a type of checked code or of the framework:
- where it implements an interface or an abstract class of a checked library;
- where it made a delegate or a function pointer that the project calls;
- where it reads an object of a checked class that holds a non-copyable value.
That code can copy a value of the non-copyable type, and no call site shows it. It can do so only if its assembly references the assembly that declares the type, or an assembly that lets it get at a value. So you get the warning for that reference, once for each assembly.
Which Types Count
A type counts if a [NonCopyable] mark makes it non-copyable, or if a noncopyable line of the project names it and the type is of checked code. In either case, no copyable line may take it out. The assembly that declares the type can be checked or not, and a reference to an assembly that forwards the type to it counts as well.
Values That Checked Code Lets Out
Some non-copyable values need no reference to a declaring assembly:
- The framework's built-in non-copyable types, such as SpinLock and GCHandle. Every assembly references the framework.
- A struct of unchecked code that only a
noncopyableline makes non-copyable. - A value that is bound to a marked type parameter.
For those, the reference that counts is one to an assembly of checked code whose members let other code get at such a value:
| A member that another assembly can reach | Example |
|---|---|
| A field that carries the value | public SpinLock Lock; |
| A member that returns a reference to the value | public ref SpinLock Lock => ref _lock; |
| A member that returns a container, or another object that carries the value | public List<GCHandle> Handles { get; } |
| A member that other code can implement and that is given a reference or a container (an interface member, a virtual member, a delegate) | void Run(ref SpinLock gate); |
| A member that calls other code back with a reference or a container (an event, or a parameter of a delegate, function pointer or interface type) | void Each(Action<List<SpinLock>> callback); |
| A base class or an interface that carries the value | class Locks : List<SpinLock> |
A value that a member returns or takes by value does not count. What a member of checked code returns by value is a new value, and a value that it is given by value belongs to it from then on. So a delegate that takes the value itself, such as Action<SpinLock>, does not count either.
A member counts where every assembly can reach it: where it is public, or protected in a class that another class can derive from. A library that forwards a type to another library counts through the library that the type moved to.
Whether the framework's non-copyable types count depends on the project that is being built. A project that references the package itself checks them, and a project that gets the analyzers only through a library does not: see Built-in Types.
Internal members are a case of their own: a project that references a library cannot see them, so the analyzer cannot tell what they let out. An assembly that the library of checked code names in an InternalsVisibleTo attribute can use them. So such an assembly gets the warning whatever the library holds, if it was not built with the analyzer.
Reference Assemblies
For a project of the same solution, the compiler is given a reference assembly: a copy with no method bodies, which names only the assemblies that the project's public and protected members use. A package can come with one too. What the code of such an assembly uses inside its methods is not visible.
So when the build is given only a reference assembly, and that assembly was not built with the analyzer, you get the warning if a non-copyable value is within its reach. That is the case when another assembly that it can reference declares a non-copyable type or lets a value out. The message names that other assembly:
warning SCA1025: Assembly 'Contoso.Helpers' was not built with the analyzer, and this build is given only its reference assembly,
which does not show what its code uses: its code can use 'Contoso.Locks', which declares a non-copyable type, and copy a
non-copyable value that it gets at there, which no call shows. ...
What is within reach depends on where the assembly comes from:
| The assembly comes from | Within its reach |
|---|---|
| A project of the solution | Every other assembly that the build references |
| A package | The assemblies of the packages that it depends on, and those of its own package |
The code of a package can use only the packages that it depends on, and NuGet lists those for the project. So a package such as SkiaSharp, which comes with reference assemblies and depends on none of your libraries, gets no warning. A build without NuGet's project.assets.json file, such as one that uses packages.config, judges a package like a project.
Turn the analyzer on in such a project. For an assembly that you cannot build with it, such as one of a package, vouch with a trusted line. An assembly of the framework gets no such warning.
The warning for a project is cautious, since the analyzer cannot see what the project uses. It also comes where the project uses nothing of the other assembly, and where the other one is built on it.
How an Assembly Ends Up Unchecked
| Cause | Example |
|---|---|
| The library that it uses kept the analyzer to itself | PrivateAssets="all", which is the default for the analyzers package. See Sharing Your Library |
| Its build turned the analyzers off | RunAnalyzers or RunAnalyzersDuringBuild set to false |
| It is written in another language, or references the assembly and not the package | |
| It was built with a version of the analyzer from before the marker |
A trusted line that has no effect gets a warning (SCA1042). That is a line for an assembly that the project does not reference, for the project's own, or for one that was built with the analyzer. It is also a line for an assembly that would get no warning to begin with: it references no assembly that declares a non-copyable type or lets a value out, no library of checked code names it in an InternalsVisibleTo attribute, and the line answers no SCA1009 for a class of yours.
See Also
These articles explain the rule behind this warning:
- Code Not Built With the Analyzer - the marker of an analyzed build, and what the rules ask of unchecked code.
- Symbol List - symbol list files and the
trustedline. - Sharing Your Library - passing the analyzer on to the projects that use a library.
Didn't add this analyzer yourself? See Why Am I Seeing This?