Sonar-dotnet: Rule S5034: "ValueTask" should be consumed correctly

Created on 9 Nov 2018  路  5Comments  路  Source: SonarSource/sonar-dotnet

Description

There don't appear to be rules that cover the safe usage of ValueTask<T>. These rules have recently been documented on the .NET Blog (there may be other locations).

Repro steps

See the linked blog post for details including code samples, but in summary code containing ValueTask<T> where the following operations are performed:

  • Awaiting a ValueTask / ValueTask<TResult> more than once
  • Awaiting a ValueTask / ValueTask<TResult> concurrently
  • Using .GetAwaiter().GetResult() when the operation may not have completed

Expected behavior

Warnings at appropriate levels.

Actual behavior

No warnings.

Known workarounds

None known.

Related information

  • SonarC# Version 7.8
  • Visual Studio Version 2017
  • If running through the Scanner for MSBuild, its version 4.4.2.1543
C# New Feature

Most helpful comment

Thanks @andrei-epure-sonarsource and team!

All 5 comments

Hi @alexangas,

Here is the rule we will implement based on the article you have provided: RSPEC-5034.

According to the docs "The use of the ValueTask type is supported starting with C# 7.0, and is not supported by any version of Visual Basic.", thus I am removing VB from this

Thanks @andrei-epure-sonarsource and team!

The current implementation raises a False Positive on the following code:

if (!awaitable.IsCompleted)
{
    return;
}聽
var readableBuffer = awaitable.GetAwaiter().GetResult().Buffer; // issue raised here

Nevermind. This is a True Positive. The code is not checking that the operation completed AND succeeded. Thus the call to awaitable.GetAwaiter().GetResult() will raise an exception if the operation failed.

Was this page helpful?
0 / 5 - 0 ratings