Not sure if you'd prefer a separate ticket for this, but I did find a problem with events (or rather, event handler).
Visual Studios CodeLens shows 1 reference, but CRR insists that the event handler is unused.
This only happens for empty event handlers and event handlers that throw exceptions (but don't do anything else).
C#public event EventHandler<EventArgs> EmptyHandler;
public event EventHandler<EventArgs> ThrowingHandler;
public void Test()
{
EmptyHandler += OnEmptyHandler;
ThrowingHandler += OnThrowingHandler;
EmptyHandler?.Invoke(this, EventArgs.Empty);
ThrowingHandler?.Invoke(this, EventArgs.Empty);
}
private void OnEmptyHandler(object sender, EventArgs ea) // CRR0026
{
// intentionally left blank
}
private void OnThrowingHandler(object sender, EventArgs ea) // CRR0026
{
throw new NotSupportedException("This should not happen for " + sender.ToString());
}
I can get behind the first one (because doing nothing in the event handler looks strange; and in fact the only place where this happens for me has a big "TODO" next to it), but I'm not too sure about the second one. Doing anything else than just the exception in there (like a Console.WriteLine, a null-check or anything) makes the message disappear.
Use case for the second kind of event handler is a "something happened" event that should log in one place, do nothing in another but abort the action in a third location (which all use the same event raiser, but handle it differently).
It feels as if the second case should specifically check for NotImplementedException on a generated event handler that is yet to be updated with proper code; but instead triggers for any kind of exception being thrown unconditionally.
Hi Emanuel, We agree these are incorrect diagnostic notifications in both cases. We decided to implement the "Empty Event Handler" diagnostic for empty event handlers and don't show diagnostic for handlers with "throw" statements, as it was in CodeRush Classic. We will try to implement such functionality as soon as possible, and when we will have any progress on the issue, we notify you in context of this ticket.