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

Fix S5034 FP: incorrect "ValueTask should be consumed correctly" in Task.WhenAll with different methods on same class instance #6779

Open Basssiiie opened 1 year ago

Basssiiie commented 1 year ago

Description

Rule S5034 reports a false positive when consuming two different ValueTasks from different methods on the same class, within a Task.WhenAll() and with a AsTask().

Repro steps

class ExampleService
{
    public ValueTask DoThing() => ValueTask.CompletedTask;
    public ValueTask DoAnotherThing() => ValueTask.CompletedTask;
}

class Program
{
    public async Task Main()
    {
        var service = new ExampleService();

        await Task.WhenAll(
            service.DoThing().AsTask(), // <- S5034 error here, even though both lines use different methods.
            service.DoAnotherThing().AsTask()
        );
    }
}

Expected behavior

No warnings here.

Actual behavior

SonarQube/SonarCloud reports the aforementioned warning on the first line within the Task.WhenAll(), even though both ValueTask's are different as they are returned from different methods.

Related information

Hope it helps. Let me know if there are any further questions. :)

gregory-paidis-sonarsource commented 1 year ago

Thank you for opening this issue @Basssiiie .

I can confirm that this is indeed a False Positive. I added a reproducer in our testcases and included this issue to our backlog.

Kind Regards, Greg.

Basssiiie commented 1 month ago

Hello @gregory-paidis-sonarsource

Thank you. Do you have an idea of where this is on the backlog? It's about a year and a half later and we still have this false positive occurring in our code base.

Thanks! 🙂

gregory-paidis-sonarsource commented 1 month ago

Hey there again! Unfortunately, I cannot give you an estimate. When we do our next FP/FN hardening sprint, I will try to prioritize this.

Basssiiie commented 1 month ago

@gregory-paidis-sonarsource Thanks! 🙂