Created
September 9, 2025 06:36
-
-
Save greatb/a5ebdd52d39b9e9fb445509c706111ad to your computer and use it in GitHub Desktop.
Console app to find the missing await on any *Async call the soluation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CodeAnalysis; | |
using Microsoft.CodeAnalysis.CSharp; | |
using Microsoft.CodeAnalysis.CSharp.Syntax; | |
using Microsoft.CodeAnalysis.MSBuild; | |
var solutionPath = args.Length > 0 ? args[0] : @"..\yourSolution.sln"; | |
using var workspace = MSBuildWorkspace.Create(); | |
var solution = await workspace.OpenSolutionAsync(solutionPath); | |
var findings = new List<string>(); | |
foreach (var project in solution.Projects) | |
{ | |
var compilation = await project.GetCompilationAsync(); | |
foreach (var doc in project.Documents) | |
{ | |
var root = await doc.GetSyntaxRootAsync(); | |
if (root is null) continue; | |
var invocations = root.DescendantNodes().OfType<InvocationExpressionSyntax>(); | |
foreach (var inv in invocations) | |
{ | |
if (inv.Expression is not MemberAccessExpressionSyntax ma) continue; | |
if (!ma.Name.Identifier.Text.EndsWith("Async")) continue; | |
// Skip if awaited | |
if (inv.Parent is AwaitExpressionSyntax) continue; | |
// Skip if directly returned | |
if (inv.Parent is ReturnStatementSyntax) continue; | |
// Skip if assigned | |
if (inv.Parent is AssignmentExpressionSyntax or EqualsValueClauseSyntax or ArgumentSyntax) | |
{ | |
// (Argument skip is heuristic; refine for WhenAll etc.) | |
continue; | |
} | |
// Skip if part of a variable declaration | |
if (inv.Parent?.Parent is VariableDeclaratorSyntax) continue; | |
var location = inv.GetLocation(); | |
var lineSpan = location.GetLineSpan(); | |
var line = lineSpan.StartLinePosition.Line + 1; | |
findings.Add($"{project.Name} | {doc.Name} | line {line} | {ma.Name.Identifier.Text}"); | |
} | |
} | |
} | |
Console.WriteLine("Potential un-awaited async calls:"); | |
foreach (var f in findings.Distinct()) | |
Console.WriteLine(f); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment