False positive csharpsquid:S3264

Hello.
I have a short C# (dotnet 7) code sample here that is being marked as major issue. The code is being scanned by SonarCloud.

Why is it being marked as an unused event?

The code sample:

public class TestClass
{
    public event EventHandler<TestArgs>? TestEvent;

    public void RegisterEventHandler(EventHandler<TestArgs> handler)
    {
        TestEvent += handler; 
    }

    public TestClass()
    {
        TestEvent?.Invoke(this, new() {CorrelationId = 3});
    }
}

public class TestArgs : EventArgs
{
    public int CorrelationId { get; set; }
}

Hello @gabor-kovac,
thanks a lot for reporting this.

Indeed this issue should not be raised as the event is invoked later on in the code.
From my side, I cannot reproduce this.
Could you please tell me which version of MSBuild you use and give me the begin, build and end step commands that you are using?

Thank you.

Hi.
The code is scanned using automatic analysis on SonarCloud.

Hey Gabor - is your repo public so I can take a look?
Thanks!

No unfortunately the code is in a private repository.

Hello again!

Most probably the issue comes from automatic analysis, which has less precision than CI-based analysis.

You should be probably getting this warning as well:
image

Ideally, to get the best results you should set-up CI based analysis (see docs),

We are working continuously on improving automatic analysis, but it is still fairly new, and for now, there is a tradeoff between ease of use and quality of diagnostics.

Hope this helps!

Thank you for you answer.
I have set up dotnet SonarScanner through a Github Actions workflow and the mentioned problem did not occur again.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.