SCA1032: Container Parameter Kept Without the Kept Mark
Mark a container parameter that the method keeps.
Example
void Register(List<LockGuard> list) => _all = list; // SCA1032: keeps it
void Register([Kept] List<LockGuard> list) => _all = list; // OK
How to Fix
- Mark the parameter
[Kept], if the method keeps the container. Its callers are then told that it is no longer theirs alone. - Do not store the container, if the method should only use it during the call.
- In an iterator or an
asyncmethod, use the container only before the firstawait, or mark the parameter[Kept].
Details
A caller assumes that a container it passes is still its own afterwards. So a method that keeps a container parameter which is not marked [Kept] gets a warning, once for each parameter. A method keeps a parameter when it:
- assigns it to a field, an element or a property, alone or inside a tuple;
- writes it through a reference;
- captures it in a lambda or a delegate;
- passes it on to something that keeps it.
A lambda that keeps its own parameter gets no warning where the delegate that it is made as keeps what it is given, by [Kept] or by a type parameter as the type of its parameter: see Lambdas.
Uses That Are Not Known to Leave It With the Caller
The analyzer checks the container's uses against those that it knows to be safe, so a use that it does not know gets a warning too, with a text of its own. One of these is passing the container to a member on the symbol list whose parameter mentions a type parameter that is not marked, since nothing checks what such a member does with it:
void Recycle(LockGuard[] buffer) => ArrayPool<LockGuard>.Shared.Return(buffer); // SCA1032
The message names the borrows entry to add to a symbol list file if the member keeps nothing.
Methods That Go On After They Return
You also get this warning for an iterator that uses a container parameter, and for an async method that uses one on a path after an await. Both keep running after they have returned to their caller.
See Also
These articles explain the rule behind this warning:
- Keeping a Container That Is Passed In - what keeps a container, and the Kept mark.
- What the Keeping Check Follows - every route by which a method can keep a container.
Didn't add this analyzer yourself? See Why Am I Seeing This?