Fixing Common Warnings
Most warnings are fixed in one of a few ways. Find the situation that matches your code: each one shows the warning and the fix side by side.
Collections
Element of a List
Read it through a span. CollectionsMarshal.AsSpan(list) gives a span over the list's own storage, so you can refer to an element instead of copying it.
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 inPlace = ref CollectionsMarshal.AsSpan(guards)[0];
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
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 = byKey[key]; // SCA1030: a copy, and the dictionary still has the guard
if (byKey.Remove(key, out var taken)) taken.Dispose(); // OK: taken out
int[] keys = [.. byKey.Keys]; // OK: the keys only
foreach (int k in keys) { if (byKey.Remove(k, out var each)) each.Dispose(); }
More on collections, and on what the analyzer knows about each one: Lists, Dictionaries and Spans.
Fields and Variables
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
Variable That Is Used After It Was Handed On
Read what you need first. A variable is a new value where it is used for the last time, so the hand-over has to be that last use.
_held = guard; // SCA1001: 'guard' is read again below
Log(guard.Id);
int id = guard.Id;
_held = guard; // OK: the last use
Log(id);
Declarations
Guard Through an In Parameter
Behind an in parameter, a member that is not readonly runs on a copy. Take the guard by ref instead:
static void Close(in LockGuard guard) => guard.Dispose(); // SCA1006: disposes a copy
static void CloseByRef(ref LockGuard guard) => guard.Dispose(); // OK
Property of a Guard Type
The getter of an auto-property returns a copy of its backing field. Use a field:
public LockGuard Guard { get; set; } // SCA1011: the getter hands out a copy
private LockGuard _guard; // OK: a field
Struct That Holds a Guard
A struct is copied with everything in it, so a struct that holds a guard has to be marked too:
struct Holding { LockGuard _guard; } // SCA1010: a struct that holds a guard must be marked too
[NonCopyable]
struct MarkedHolding { LockGuard _guard; } // OK
Loop Over an Array
A foreach with a by-value variable copies each element. Loop with a ref variable over a span of the array:
foreach (LockGuard guard in array) guard.Dispose(); // SCA1001: each 'guard' is a copy
foreach (ref LockGuard guard in array.AsSpan()) guard.Dispose(); // OK
More Cases
Three more cases have a guide of their own:
- A callback that works on a guard, which needs a delegate with a
refparameter: see Callbacks. - A constructor that validates and then takes, which needs a
[MovedFrom] refparameter: see Constructors That Validate and Then Take. - A task that a waiter completes later, made with a
TaskCompletionSource: see Making a Task for a Waiter.
Note
A few shapes of correct code still get a warning, because the analyzer follows a value through one method at a time. Correct Code That Still Gets a Warning shows each one, with the clean form or the suppression to use.
Next Steps
Here is where to go from here:
- New and Stored Values - The idea behind every one of these fixes.
- Why Am I Seeing This? - If the warnings came with a library that you use.
- All Diagnostics - The article for every ID.