Singulink.CodeAnalysis.Analyzers
1.0.2
Prefix Reserved
dotnet add package Singulink.CodeAnalysis.Analyzers --version 1.0.2
NuGet\Install-Package Singulink.CodeAnalysis.Analyzers -Version 1.0.2
<PackageReference Include="Singulink.CodeAnalysis.Analyzers" Version="1.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Singulink.CodeAnalysis.Analyzers" Version="1.0.2" />
<PackageReference Include="Singulink.CodeAnalysis.Analyzers"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Singulink.CodeAnalysis.Analyzers --version 1.0.2
#r "nuget: Singulink.CodeAnalysis.Analyzers, 1.0.2"
#:package Singulink.CodeAnalysis.Analyzers@1.0.2
#addin nuget:?package=Singulink.CodeAnalysis.Analyzers&version=1.0.2
#tool nuget:?package=Singulink.CodeAnalysis.Analyzers&version=1.0.2
Singulink.CodeAnalysis
Singulink.CodeAnalysis is a set of Roslyn analyzers that find every place where code copies a struct that must never be copied. A lock
guard, a handle or a pooled buffer is a struct where each instance owns something that only one instance may own: copy it, and two instances
release the same lock or free the same handle. Mark such a struct with [NonCopyable], and the build warns wherever it is copied, even
where the copy is easy to miss: a read out of a list or a task, a value passed through generic code, and the copies that the compiler makes
by itself for patterns, iterators and read-only references.
Moving a value is not a copy, because one instance is left afterwards. A local that you pass on and do not use again moves, and
NonCopyable.Move takes a value out of a field and leaves the field empty. See
Moving Values.
The analyzer checks only what is marked, and there are no style or opinion rules. All the rules so far, the SCA1000s, are about non-copyable structs.
using Singulink.CodeAnalysis;
[NonCopyable]
public struct LockGuard : IDisposable
{
// ...
}
var guard = myLock.Enter(); // OK: a new value
Console.WriteLine(guard.Id); // OK: used in place
TakeGuard(ref guard); // OK: passed by reference
TakeGuard(guard); // OK: 'guard' is not used again, so the guard moves
var copy = _guard; // SCA1001: a copy of the field
TakeGuard(_guard); // SCA1003: passed by value, and the field still holds it
TakeGuard(NonCopyable.Move(ref _guard)); // OK: moved, and '_guard' is left empty
var other = myLock.Enter();
TakeGuard(other); // a copy, since 'other' is used below
other.Dispose(); // SCA1016: used after it was handed over
var guards = new List<LockGuard>();
guards.Add(myLock.Enter()); // OK: a new value is moved in
LockGuard first = guards[0]; // SCA1030: a copy out of the list
LockGuard[] all = guards.ToArray(); // SCA1020: ToArray copies every element
What is guaranteed is one instance, not one call of Dispose. In code that the analyzer checks, a value of a marked type is never
in two places. Nothing guarantees that a value is disposed, or disposed only once: a guard should clear itself in Dispose, so that a
second call on the same instance does nothing. A readonly struct guard cannot do that.
A type that the project takes out with a
copyable line of a symbol list file is an
ordinary struct to the analyzer, and the guarantee does not cover it.
What it cannot see is small. What is left takes unsafe code, reflection or dynamic, an API used against its documentation, or something that
you vouched for. See
What Is Not Covered.
Documentation: guides, an article for every warning and the fully documented API are on the project documentation site.
About Singulink
We are a small team of engineers and designers dedicated to building beautiful, functional and well-engineered software solutions. We offer very competitive rates as well as fixed-price contracts and welcome inquiries to discuss any custom development / project support needs you may have.
This package is part of our Singulink Libraries collection. Visit https://github.com/Singulink to see our full list of publicly available libraries and other open-source projects.
Why Am I Seeing This?
You may not have added this analyzer yourself. A library whose public API has structs that must not be copied, such as lock guards or handles, can bring the analyzer with it, so that the projects that use the library get a warning when they copy those structs.
The analyzer then checks the types that the library marked. Even with nothing marked in your own code, you get a warning where your code
copies one of the library's structs, or a collection or a task that holds one. In such a project, the analyzer does not check the
built-in non-copyable types, such as GCHandle.
Four warnings are about code that can copy such a struct without a visible copy, so they can appear even in a project that copies nothing:
MemberwiseClonein a class that another assembly can derive from (SCA1008);- a field that holds such a struct in a class that derives from a class that clones itself, such as
Encoding, or such a struct bound to a generic class that derives from one (SCA1009); - any use of
RuntimeHelpers.GetObjectValue(SCA1007); - a referenced assembly that uses the library's structs and was not built with the analyzer (
SCA1025, a message and not a warning in such a project).
Each warning has an ID of the form SCA1001, and its message names the value, says what receives the copy, and says what to do instead. The
ID links to the warning's article on the documentation site, which shows an example that gets the warning and
the forms of the code that do not. Most warnings are fixed in one of a few ways, shown under Common Fixes below.
New Values and Stored Values
Every rule comes down to one distinction:
- A new value is one that nobody else holds:
new(),default, the result of a call or of an operator, the result ofNonCopyable.Move, and a local or a by-value parameter where it is used for the last time. It can go anywhere once: into a variable, a field, a parameter, a tuple, an array, a collection, a hole of an interpolated string. - A stored value is anything read from a local, a parameter, a field, an array element or a reference. Reading it by value makes a copy, and
the copy is what gets the warning. It can be used in place (
guard.Dispose()), passed byref,inorref readonly, and referred to with areflocal or arefreturn.
See New Values and Stored Values for the rest of the distinction: the result of an assignment, the last use of a variable, and what else is stored.
Common Fixes
Most warnings are fixed in one of a few ways.
An element of a list: read it through a span, which gives a reference to the element.
LockGuard first = guards[0]; // SCA1030: a copy out of the list
guards[0].Dispose(); // SCA1030: disposes a copy
CollectionsMarshal.AsSpan(guards)[0].Dispose(); // OK: in place
ref LockGuard held = ref CollectionsMarshal.AsSpan(guards)[0];
A loop over the elements: declare a ref variable over a span of them.
foreach (var guard in guards) guard.Dispose(); // SCA1030: each 'guard' is a copy
foreach (ref var guard in CollectionsMarshal.AsSpan(guards)) guard.Dispose(); // OK
foreach (ref var guard in array.AsSpan()) guard.Dispose(); // OK
A value in a dictionary: take it out with the member that removes it. To release every value, take a snapshot of the keys first.
LockGuard guard = held[key]; // SCA1030: a copy, and the dictionary still has the guard
if (held.Remove(key, out var taken)) taken.Dispose(); // OK: taken out
int[] keys = [.. held.Keys]; // OK: the keys only
foreach (int k in keys) { if (held.Remove(k, out var each)) each.Dispose(); }
A field that is handed on: move it out, which leaves the field empty.
return _guard; // SCA1002
return NonCopyable.Move(ref _guard); // OK
Take(_guard); // SCA1003
Take(NonCopyable.Move(ref _guard)); // OK
A variable that is used after it was handed over: read what you need first. A variable is a new value where it is used for the last time, so the hand-off has to be that last use. The warning is at the use after the hand-off.
_held = guard; // a copy, since 'guard' is read again below
Log(guard.Id); // SCA1016: used after it was handed over
int id = guard.Id;
_held = guard; // OK: the last use
Log(id);
A callback that works on a guard: Func<> and Action<> take their arguments by value, and an async lambda cannot have a ref
parameter. Declare a delegate with a ref parameter. For async work over a guard, keep the guard in an object and pass that.
public delegate TResult GuardFunc<TResult>(ref LockGuard guard);
public TResult With<TResult>(GuardFunc<TResult> body)
{
var guard = Enter();
try { return body(ref guard); }
finally { guard.Dispose(); }
}
int count = locks.With((ref LockGuard guard) => guard.Count);
A constructor that validates and then takes: take the value through a [MovedFrom] ref parameter, so that the caller still has it when
the constructor throws. Read it and clear it in two statements with nothing between them.
try { return new UnitOfWork(scope, user); } // the 'catch' can still run, so this is not the last use...
catch { scope.Dispose(); throw; } // SCA1016: ...and the 'catch' uses 'scope' after the hand-off
public UnitOfWork([MovedFrom] ref TransactionScope scope, string user)
{
ArgumentException.ThrowIfNullOrEmpty(user); // throws before anything is taken
_scope = scope;
scope = default;
}
try { return new UnitOfWork(ref scope, user); } // OK
catch { scope.Dispose(); throw; }
A task that a waiter completes later: make the TaskCompletionSource in the method that returns its task, and read Task once, there.
See What a Member on the Symbol List Hands Out.
var source = new TaskCompletionSource<LockGuard>();
_waiters.Enqueue(source);
return source.Task; // OK: made here, read once
Three shapes of correct code still get a warning, with the clean form or the suppression for each: see Correct Code That Still Gets a Warning.
Turning Things Down
One type that you copy on purpose: when the built-in framework symbol list or a library makes a struct non-copyable, take it
out with a copyable line in a symbol list file. That is a file named NonCopyableSymbols.txt in the project folder, and
the analyzer reads it with no further setup:
T:System.Runtime.InteropServices.GCHandle;copyable
Most projects never need such a file: .NET's own libraries are covered by the framework symbol list, and your own code uses attributes.
A rule: every rule is a warning, and you turn it down like any other analyzer rule, in an .editorconfig file:
[*.cs]
dotnet_diagnostic.SCA1023.severity = none
One warning on correct code: suppress it where it stands, with a comment that says why it is right. See Turning Rules Down and Symbol List.
The updated memory safety rules of C#: in a project that turns them on, a report about a struct marked
[NonCopyable(UnsafeToCopy = true)] is an error, and only code in an unsafe context or a declaration marked safe or unsafe can
suppress one. Every other non-copyable type gets the same warnings as under the current rules. See Updated Memory Safety Rules.
Marking a Struct of Your Own
Reference Singulink.CodeAnalysis and mark the struct with [NonCopyable], as in the example above. The analyzer recognizes the
attribute by its full name, so a project that wants no runtime dependency can declare the attributes itself, in the namespace
Singulink.CodeAnalysis, and reference only Singulink.CodeAnalysis.Analyzers. See
Marking a Struct, and Libraries for passing
the analyzer on to the projects that use your library.
Installation
There are two packages:
| Package | Contents |
|---|---|
Singulink.CodeAnalysis |
The attributes, such as Singulink.CodeAnalysis.NonCopyableAttribute, the NonCopyable.Move helper, and the checks NonCopyable.ThrowIfNullRef and NonCopyable.ThrowIfNullRefArgument, in assemblies for .NET Standard 2.0 and .NET 10. It depends on Singulink.CodeAnalysis.Analyzers, so referencing it brings the analyzer too. |
Singulink.CodeAnalysis.Analyzers |
The analyzer only. It has no runtime assembly and no dependencies, so it adds nothing to your build output. |
Install Singulink.CodeAnalysis in most projects. An update then brings the attributes, the helpers and the analyzer together:
<PackageReference Include="Singulink.CodeAnalysis" Version="1.0.2" />
Install Singulink.CodeAnalysis.Analyzers alone if you declare the attributes yourself (see Marking a Struct of Your Own above) and want
no runtime dependency:
<PackageReference Include="Singulink.CodeAnalysis.Analyzers" Version="1.0.2" PrivateAssets="all" />
In a project that references one of the two packages directly, the analyzer also checks the built-in non-copyable types, a few structs of
the framework such as GCHandle and SpinLock. In a project that gets the analyzer only through a library, it does not. See
Non-copyable Types and
Leaving out the built-in non-copyable types.
The analyzer package is marked as a development dependency, so dotnet add package and Visual Studio add it with PrivateAssets="all", which keeps it
to your own project. To let it reach the projects that use your library, see Libraries.
Requirements: C# projects built with the .NET 10 SDK or later, that is, a compiler of version 5.0 or later. The analyzer does not load in an older compiler. The target framework of the project does not matter.
The analyzer is tested with the compilers of the .NET 10 and .NET 11 SDKs. With a newer SDK, you get a warning on new syntax that the analyzer does not know, as a use that is not known to be safe. A copy that a newer compiler makes inside syntax that the analyzer already knows is covered once that SDK has been tested.
Further Reading
Here are some links to get you started:
- Getting Started - A walkthrough: install the analyzer, mark a first struct and read a first warning.
- The Diagnostics - Every ID, each with an article on what the warning means and how to fix it.
- New Values and Stored Values - The concepts behind the rules.
- API Documentation - The attributes and the helpers.
- Design Notes - Why the analyzer works as it does, for contributors.
- Chat on Discord - Have questions or want to discuss the library? This is the place for all Singulink project discussions.
- Third-party notices - The non-copyable analyzer
is derived from Roslyn's
DoNotCopyValueanalyzer, and some of its tests use the test sources of hamarb123.Analyzers, both under the MIT license.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Singulink.CodeAnalysis.Analyzers:
| Package | Downloads |
|---|---|
|
Singulink.CodeAnalysis
Attributes that mark types and members for the Singulink.CodeAnalysis analyzers, such as [NonCopyable] for structs that must not be copied, and the NonCopyable.Move helper. Brings the analyzers with it. |
GitHub repositories
This package is not used by any popular GitHub repositories.