SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
797 stars 229 forks source link

S3168 feature suggestion: Async void event delegate with "On" prefix should raise if it's not an event callback #9682

Closed ryanachten closed 1 month ago

ryanachten commented 1 month ago

Description

We've found that the current rule for S3168 doesn't reliably report issues for async/void usage. This appears to be due to excluding method names that start with "On" on the assumption that these refer to event handler delegates as per Microsoft guidance.

The issue is, there are many scenarios where "On" is used as a prefix in methods unrelated to event delegates, and in these scenarios the SonarQube rule fails to report such code issues. To this end, I have the following questions:

Repro steps

Create an async void method with an "On" prefix. See Actual behavior below for an example.

Expected behavior

Given a method unrelated to an event handler delegate, an S3168 rule violation should be raised:

public async void OnResultExecuted(ResultExecutedContext context) // S3168 raised
{
    // ...
}

Actual behavior

Given a method unrelated to an event handler delegate, an S3168 rule violation is not raised:

public async void OnResultExecuted(ResultExecutedContext context) // No S3168 raised
{
    // ...
}

Known workarounds

Related information

gregory-paidis-sonarsource commented 1 month ago

Hey there,

I am afraid this is not possible. The problem is that it is virtually impossible to separate non-event handling OnXXX methods to event-handler ones.

To do that, we would need to traverse the call graph with our Symbolic Execution engine which would really affect performance. For now we only go method by method. We made the choice to exclude OnXXX methods, as they are usually event handler callbacks, and False Negatives are better (less noisy/annoying) than False Positives.