False positive csharpsquid:S4261

Hi,
I have a C# code (.NET 7 framework), scanned by SonarCloud (automatic scan activated, Github repo binded).

I’m receiving the csharpsquid:S4261 violation for the following code:
image

It’s a pure async function, why should I remove the Async suffix?

Hello @viktorsebok,

Thank you for your feedback.

Unfortunately, I was not able to reproduce the issue with the given code snippet.

Could you provide us with the GitHub repository and SonarCloud links?
Or, if you can’t, could you provide us with a code snippet that raises the issue?

That would help a lot in identifying the problem.

Thank you,

Hello @sebastien.marichal,
Thank you for the response…
Unfortunately we have a private GH repo, I can’t provide you any repo…
But here I created a complete class, maybe you can reproduce with this:

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Edge.Service.SystemMonitor.DAL.SystemStatus
{
    internal class TestAsyncSonar : IHostedService
    {
        #region Const
        private const int TIMER_DELAY = 2000;
        private const int TIMER_PERIODE = 60 * 1000;
        #endregion 

        #region Fields
        private readonly ILogger<TestAsyncSonar> logger;
        private readonly Timer timer;
        #endregion

        #region C'tor
        internal TestAsyncSonar(ILogger<TestAsyncSonar> logger)
        {
            this.logger = logger;
            this.logger.LogInformation("SystemStatus provider is ready...");
        }
        #endregion

        #region IResourceProvider
        public event EventHandler<IList<int>>? OnResource;

        #endregion

        #region IHostedService

        public Task StartAsync(CancellationToken cancellationToken)
        {
            this.timer.Change(TIMER_DELAY, TIMER_PERIODE);
            logger.LogInformation("Periodic system status provider started...");
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            this.timer.Change(Timeout.Infinite, Timeout.Infinite);
            logger.LogInformation("Periodic system status provider stopped...");
            return Task.CompletedTask;
        }

        #endregion


    }
}

For the StartAsync and StopAsync I got the csharpsquid:S4261 violation.
These two functions are the part of the IHostedService interface.

BR

1 Like

Hello @viktorsebok,

Unfortunately, I cannot reproduce the issue even with your reproducer.

Can you tell me how you got the issue raised on your reproducer?
Did you set up a new project?
Did you use automatic analysis as well (if on SonarCloud)?

In any case, would it be an option for you to use other analysis methods such as GitHub Actions?
I suggest also trying manually with the SonarScanner for .NET to see if the issue still raises.

Regards,