RefactoringEssentials icon indicating copy to clipboard operation
RefactoringEssentials copied to clipboard

Analyzer for wrong usage of async methods

Open Rpinski opened this issue 10 years ago • 2 comments

Example async method:

        public Task<string> DoSomethingAsync(string str)
        {
            return Task.FromResult(str);
        }

Examples of wrong usage:

        public async Task Scenario1Async()
        {
            var usedCorrectly = await DoSomethingAsync("helloworld");

            // obviously wrong, but could happen for fire-and-forget cases where you don't check eg the boolean result
            var blatantlywrong = DoSomethingAsync("get it going");
        }

        public async Task Scenario2Async()
        {
            var usedCorrectly = await DoSomethingAsync("helloworld");

            // If you have models that happen to have an Id property (very likely when working with dbs)
            var stillwrong = DoSomethingAsync("guess what");
            var x = stillwrong.Id;
        }

        public void Scenario3()
        {
            // Creatively wrong
            List<string> somestrings = new List<string>() { "Hello", "World" };
            var result = somestrings.Select(DoSomethingAsync).ToList();
            Console.WriteLine(result[1]);
        }

Rpinski avatar Jan 15 '16 14:01 Rpinski

Basically, analyzer must figure out that method is async and when being called, there wasn't any usage of the await keyword? Sounds pretty straightforward right ? @christophwille

Kavignon avatar May 29 '16 13:05 Kavignon

There is already the project asyncusage.analyzers. Maybe that is a better place to add this to.

bexxx avatar Aug 12 '16 07:08 bexxx