sonar-dotnet
sonar-dotnet copied to clipboard
Rule S3264: FP when using +=
Description
Rule S3264: a false positive is raised when chaining events.
Repro steps
namespace TestCases
{
using System;
public class EventChainingDirect
{
public event EventHandler Logging;
public void RunTest()
{
EventTestBase eventTestBase = new EventTestBase();
eventTestBase.Logging += Logging;
eventTestBase.RunSomethingThatSendAnEvent();
}
}
public class EventChainingLambda
{
public event EventHandler Logging;
public void RunTest()
{
EventTestBase eventTestBase = new EventTestBase();
eventTestBase.Logging += (s, e) => Logging(s, e);
eventTestBase.RunSomethingThatSendAnEvent();
}
}
public class EventTestBase
{
public event EventHandler Logging;
public void RunSomethingThatSendAnEvent() => Logging(this, EventArgs.Empty);
}
}
Expected behavior
The issue should not be raised in this context.
Actual behavior
EventChainingDirect.Logging
is considered non-compliant although the event is invoked by RunSomethingThatSendAnEvent
Known workarounds
Using a lambda function like in EventChainingLambda
Related information
- C#/VB.NET Plugins version: 8.21
We are facing the same issue but not even with direct chaining. For ProcessMessageAsync
event we get S3264: Remove the unused event 'ProcessErrorAsync' or invoke it.
internal class ServiceBusMessageProcessor
{
public event Func<ProcessBrokerMessageEventArgs, Task> ProcessMessageAsync;
public ServiceBusMessageProcessor(ServiceBusClient serviceBusClient, string topic, string subscriptionName)
{
var processor = serviceBusClient.CreateProcessor(topic, subscriptionName, new ServiceBusProcessorOptions());
processor.ProcessMessageAsync += OnProcessMessageAsync;
}
private async Task OnProcessMessageAsync(ProcessMessageEventArgs arg)
{
await ProcessMessageAsync?.Invoke(new ProcessServiceBusMessageEventArgs(arg));
}
}