Find non-awaited async method calls

前端 未结 5 1435
无人及你
无人及你 2020-12-24 11:36

I\'ve just stumbled across a rather dangerous scenario while migrating an ASP.NET application to the async/await model.

The situation is that I made a method async:

5条回答
  •  孤独总比滥情好
    2020-12-24 12:35

    In the end, we used roslyn to find all instances where a return value of Task or Task<> was ignored:

    if (methodSymbol.ReturnType.Equals(syntaxNodeAnalysisContext.SemanticModel.Compilation.GetTypeByMetadataName(typeof(Task).FullName)))
    {
        // For all such symbols, produce a diagnostic.
        var diagnostic = Diagnostic.Create(Rule, node.GetLocation(), methodSymbol.ToDisplayString());
    
        syntaxNodeAnalysisContext.ReportDiagnostic(diagnostic);
    }
    if (((INamedTypeSymbol) methodSymbol.ReturnType).IsGenericType && ((INamedTypeSymbol) methodSymbol.ReturnType).BaseType.Equals(syntaxNodeAnalysisContext.SemanticModel.Compilation.GetTypeByMetadataName(typeof(Task).FullName)))
    {
        // For all such symbols, produce a diagnostic.
        var diagnostic = Diagnostic.Create(Rule, node.GetLocation(), methodSymbol.ToDisplayString());
    
        syntaxNodeAnalysisContext.ReportDiagnostic(diagnostic);
    }
    

提交回复
热议问题