Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S5034 FP: ValueTask.IsCompletedSuccessfully not considered in ternary operator #9661

Open
fiotti opened this issue Sep 9, 2024 · 1 comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.

Comments

@fiotti
Copy link

fiotti commented Sep 9, 2024

Description

S5034 does not take ternary operators into consideration.

Repro steps

class Test
{
  static ValueTask<IEnumerable> MakeGeneric(ValueTask<IEnumerable<T>> task)
    => task.IsCompletedSuccessfully
      // S5034: Refactor this 'ValueTask' usage to consume the result only if the operation has completed successfully.
      ? ValueTask.FromResult<IEnumerable>(task.Result)
      : MakeGenericSlow(task);

  static async ValueTask<IEnumerable> MakeGenericSlow(ValueTask<IEnumerable<T>> task)
    => await task;
}

Expected behavior

No warning.

Actual behavior

S5034: Refactor this 'ValueTask' usage to consume the result only if the operation has completed successfully.

Known workarounds

class Test
{
  static ValueTask<IEnumerable> MakeGeneric(ValueTask<IEnumerable<T>> task)
  {
    if (task.IsCompletedSuccessfully)
      return ValueTask.FromResult<IEnumerable>(task.Result); // No warning.
      
    return MakeGenericSlow(task);
  }

  static async ValueTask<IEnumerable> MakeGenericSlow(ValueTask<IEnumerable<T>> task)
    => await task;
}

Related information

  • SonarLint for Visual Studio 2022, version 8.9.0.92083
  • Visual Studio Professional 2022 (64 bit), version 17.10.4
  • dotnet 8.0.303
  • MSBuild version 17.10.4+10fbfbf2e for .NET Framework, version 17.10.4.21802
  • Windows 11 Pro 23H2 22631.3880
@mary-georgiou-sonarsource
Copy link
Contributor

Hello @fiotti I confirm this is a false positive.
Thank you for reporting this.

@mary-georgiou-sonarsource mary-georgiou-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

No branches or pull requests

2 participants