Open sandboxFocusImprove this doc

LAMA0875: A weak reference held by a durable type should be named Dangerous

Severity Info
Reported by Metalama.Framework (Roslyn analyzer)
Message '<Member>' holds a weak reference, so its name should end in 'Dangerous' to show that the value may be absent.

Cause

A type marked [Durable] (from the Metalama.Framework.Utilities namespace) must be safe to be held across compilations, so it must not retain a compilation, a syntax tree, or a symbol. A weak reference, System.WeakReference or System.WeakReference<T>, is durable whatever it refers to, because it doesn't keep its target alive. The analyzer therefore doesn't check what the weak reference points to.

However, the target of a weak reference may be collected at any time, so the code that reads it must be prepared for the value to be absent, and is responsible for establishing that it isn't. The convention is to end the name of such a member with Dangerous, so that readers are warned.

The analyzer reports LAMA0875 on a non-static field or automatically implemented property whose type is WeakReference or WeakReference<T>, when the member belongs to a type bound by the durable contract (a type marked [Durable], or one that derives from or implements such a type) and its name doesn't end in Dangerous. In the message, <Member> is the name of the member. Members of types that aren't bound by the contract aren't reported.

This diagnostic is reported by a Roslyn analyzer, which snapshot tests of aspects don't run, so it can't be shown in an example.

Resolution

Rename the member so that its name ends in Dangerous, for instance _lastCompilation to _lastCompilationDangerous.

Since the severity is Info, you can also ignore the diagnostic or configure its severity in an .editorconfig file.

See also