GithubHelp home page GithubHelp logo

arthur-liberman / unused-task-warning Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ykoksen/unused-task-warning

0.0 0.0 0.0 72 KB

Compiler analyser to generate Warnings whenever a Task is not awaited nor assigned to a variable (also in those instances not caught by CS4014).

License: MIT License

C# 94.25% PowerShell 5.75%

unused-task-warning's Introduction

unused-task-warning

When using dependency injection and async-await pattern it is possible to end up with an interface with a method that returns a Task. If this interface method is used in a synchronous method there is a likelihood that it will erroneously be run as a fire and forget method (which will not trigger inbuilt warning CS4014). In this situation this analyser generates a warning.

Can both be used as a Visual Studio extension or preferably as a project analyser available from NuGet.

Example:

using System.Threading.Tasks;

namespace AsyncAwaitProblem
{
	public interface ICallee
	{
		bool ProblemSolved { get; }
		Task SolveProblemAsync();
	}

	public class Callee : ICallee
	{
		public bool ProblemSolved { get; set; }

		public async Task SolveProblemAsync()
		{
			await Task.Delay(10);
			ProblemSolved = true;
		}
	}
	
	public class Caller
	{
		public bool DoCall()
		{
			ICallee xxx = new Callee();

			// This analyser will give a warning at the following line
			xxx.SolveProblemAsync(); // This is most likely an undesired fire and forget. 

			return xxx.ProblemSolved; // Will return false - we expected it to return true
		}
	}
}

Note that this analyser currently only checks for current known awaitable types (ex. types Task, ValueTask and ConfiguredTaskAwaitable (the type returned when using the ConfigureAwait method)). If another 'Awaitable' type is returned this analyser will not give the warning. This might be fixed in a future version.

Available from NuGet here: https://www.nuget.org/packages/Lindhart.Analyser.MissingAwaitWarning/

Available from Visual Studio marketplace here: https://marketplace.visualstudio.com/items?itemName=Lindhart.missingAwaitWarning#overview

For information on how to use analyzers check here: https://docs.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview?view=vs-2019

unused-task-warning's People

Contributors

maxhorstmann avatar samusaran avatar ykoksen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.