sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

NET-1534 Rule Idea: `void` method implementations should not be `async`

Open Tim-Pohlmann opened this issue 1 year ago • 5 comments

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 avatar Nov 29 '24 09:11 Tim-Pohlmann

The implementation should be a straightforward adaption of S3168.

Tim-Pohlmann avatar Nov 29 '24 09:11 Tim-Pohlmann

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?

  • Methods implementing an interface
  • Methods overriding a base class method
  • Virtual methods

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

qhris avatar Nov 29 '24 12:11 qhris

I clarified it. Thanks for pointing it out!

Tim-Pohlmann avatar Nov 29 '24 15:11 Tim-Pohlmann

Internal ticket NET-1534

Similar to AsyncFixer03

Corniel avatar Jul 01 '25 07:07 Corniel