SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
Other
798 stars 229 forks source link

Rule Idea: `void` method implementations should not be `async` #9711

Open Tim-Pohlmann opened 3 days ago

Tim-Pohlmann commented 3 days ago
interface IInterface
{
    void InterfaceMethod();
}

class Base
{
    protected virtual void BaseMethod() {}
}

class Sample : Base, IInterface
{
    public async void InterfaceMethod() {}           // Noncompliant 
    protected override async void BaseMethod() {}    // Noncompliant
    protected virtual async void VirtualMethod() {}  // Noncompliant
    async void OtherMethod() {}                      // Compliant: covered by S3168
}

This rule complements S3168, which ignores methods that implement an interface, override a base method, or are virtual. Having this functionality as a separate rule allows users to tailor their analysis to their needs.

Tim-Pohlmann commented 3 days ago

The implementation should be a straightforward adaption of S3168.

qhris commented 3 days ago

This looks good to me!

Some questions regarding the exceptions raised in https://github.com/SonarSource/rspec/pull/4547:

Would this capture all these scenarios?

From the wording here it should capture all 3 cases, correct?

Tim-Pohlmann commented 3 days ago

I clarified it. Thanks for pointing it out!