Cursoriam.Analyzers 1.0.1

dotnet add package Cursoriam.Analyzers --version 1.0.1
NuGet\Install-Package Cursoriam.Analyzers -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cursoriam.Analyzers" Version="1.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cursoriam.Analyzers --version 1.0.1
#r "nuget: Cursoriam.Analyzers, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cursoriam.Analyzers as a Cake Addin
#addin nuget:?package=Cursoriam.Analyzers&version=1.0.1

// Install Cursoriam.Analyzers as a Cake Tool
#tool nuget:?package=Cursoriam.Analyzers&version=1.0.1

Discarded Task Analyzer

There are many analyzers out there but once I was hunting for a bug and I found this piece of code (simplified):

public void Method1()
{
    var cc = new CustomClass();
    _ = cc.Method2Async();
}

The method Method2Async returns a Task and this Task is immediately discarded. Needless to say this caused subtle problems in the application. For example, exceptions thrown are not observed. The garbage collector could free memory still in use.

Existing analyzers didn't catch this problem so I decided to create a new one for this issue.

This analyzer will find an unawaited discard and will suggest a fix. This fix will place an await keyword if the return type is a Task<T>. If the return type of the method is a Task (non-generic), the discard will be removed (because awaiting a Task will return a void, and a void can't be assigned to a variable). Also, the fix will try to add the async keyword to the containg method, and change the return type to Task or Task<T> if necessary.

The result will be like this:

async public Task Method1()
{
    var cc = new CustomClass();
    await cc.Method2Async();
}

Known limitations:

  • Does only provide a partial fix for methods that already return a Task, but are not async
  • Does only provide a partial fix for local functions and lambdas

Two analyzers for threading and the async/await pattern which I like and got inspiration from:

Microsoft.VisualStudio.Threading.Analyzers
Meziantou.Analyzer

The icon for this packages is from flaticon.com. Here is the attribution: Syntax icons created by alfanz - Flaticon

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 2,625 3/26/2023
1.0.0 239 3/18/2023

Fix the ordering of the modifiers