Add [RequireStaticDelegate]
annotations
#6174
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds
[RequireStaticDelegate]
annotation to places where there is anAction<T>
+T data
overload and the intent is to avoid capture.Adding this to
InvokeOnDisposal
is a no-brainer, but I'm not entirely sure about adding toScheduler
and other places. So this is an RFC of sorts.There are valid use cases of non-static delegates, so I'm thinking it would be useful to apply the attribute, but change the inspection severity to a hint -- let the programmer know that context is captured.
For cases where we want to raise an error for capturing context (eg. in
InvokeOnDisposal
),[RequireStaticDelegate(IsError = true)]
can be used. This will force an error regardless of the configured severity. Unless the severity is configured asDO_NOT_SHOW
, in which case the error is silenced.Example of setting
LambdaShouldNotCaptureContext
severity to hint. If this is considered too noisy, then we don't apply the attribute inScheduler
, etc.Inspections that can be configured
LambdaExpressionMustBeStatic
Active when the lambda doesn't capture context, but isn't
static
.I think this is best set to
hint
(defaults tosuggestion
).LambdaShouldNotCaptureContext
Active when the lambda captures context (eg. variables,
this
):Including when a method is passed and
this
is implicitly captured: