An event is raised by calling it directly, which throws when nobody has subscribed
What it finds
A field-like event is null until the first handler subscribes, so Changed(this, e) throws NullReferenceException when there is none. Changed?.Invoke(this, e) raises it only when there are handlers. The rule reports direct calls of an event of the same type in a member that never compares the event with null.
From the rule's source, event_raised_without_subscribers_check.rs.
using System; class Button{ public event EventHandler Clicked; public void Click() { Clicked(this, EventArgs.Empty);tx0 reports: the event Clicked is raised directly and throws without subscribers } public void SafeClick() { Clicked?.Invoke(this, EventArgs.Empty); }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:event-raised-without-subscribers-check" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:event-raised-without-subscribers-check] reason="..."tx0 explain tx0/csharp:event-raised-without-subscribers-check shows the same in the terminal. Found a false positive? Report it.