AsyncVoidAnalyzer

We found 10 examples in language CSharp for this search. You will see 25 fragments of code.
        public IEnumerable<string> GetFixableDiagnosticIds()
        {
            return new[] { AsyncVoidAnalyzer.AsyncVoidId };
        }

        public sealed override ImmutableArray<string> GetFixableDiagnosticIds()
        {
            return ImmutableArray.Create(AsyncVoidAnalyzer.AsyncVoidId);
        }

        public sealed override ImmutableArray<string> GetFixableDiagnosticIds()
        {
            return ImmutableArray.Create(AsyncVoidAnalyzer.AsyncVoidId);
        }

        public sealed override ImmutableArray<string> GetFixableDiagnosticIds()
        {
            return ImmutableArray.Create(AsyncVoidAnalyzer.AsyncVoidId);
        }

        public sealed override ImmutableArray<string> GetFixableDiagnosticIds()
        {
            return ImmutableArray.Create(AsyncVoidAnalyzer.AsyncVoidId);
        }

        public sealed override FixAllProvider GetFixAllProvider()
        {
            return null;
        }

        public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnosticSpan = context.Diagnostics.First().Location.SourceSpan;

            // Find the type declaration identified by the diagnostic.
            var methodDeclaration = root.FindToken(diagnosticSpan.Start).Parent.FirstAncestorOrSelf<MethodDeclarationSyntax>();

            // Return a code action that will invoke the fix.
            return new[] { new AsyncVoidCodeAction("Async methods should not return void", c => VoidToTaskAsync(context.Document, methodDeclaration, c)) };
        }

        private async Task<Document> VoidToTaskAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
        {
            // The Task object must be parsed from a string using the Syntax Factory
            var newType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task").WithAdditionalAnnotations(Simplifier.Annotation).WithTrailingTrivia(methodDeclaration.ReturnType.GetTrailingTrivia());

            var newMethodDeclaration = methodDeclaration.WithReturnType(newType);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var newRoot = oldRoot.ReplaceNode(methodDeclaration, newMethodDeclaration);
            var newDocument = document.WithSyntaxRoot(newRoot);

            // Return document with transformed tree.
            return newDocument;
        }

            protected override Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
            {
                return this.createDocument(cancellationToken);
            }


        public sealed override FixAllProvider GetFixAllProvider()
        {
            return null;
        }

        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            // Find the type declaration identified by the diagnostic.
            var methodDeclaration = root.FindToken(diagnosticSpan.Start).Parent.FirstAncestorOrSelf<MethodDeclarationSyntax>();

            // Register a code action that will invoke the fix.
            context.RegisterCodeFix(
                new AsyncVoidCodeAction("Async methods should not return void",
                                        c => VoidToTaskAsync(context.Document, methodDeclaration, c)),
                diagnostic);
        }

        private async Task<Document> VoidToTaskAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
        {
            // The Task object must be parsed from a string using the Syntax Factory
            var newType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task").WithAdditionalAnnotations(Simplifier.Annotation).WithTrailingTrivia(methodDeclaration.ReturnType.GetTrailingTrivia());

            var newMethodDeclaration = methodDeclaration.WithReturnType(newType);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var newRoot = oldRoot.ReplaceNode(methodDeclaration, newMethodDeclaration);
            var newDocument = document.WithSyntaxRoot(newRoot);

            // Return document with transformed tree.
            return newDocument;
        }

            protected override Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
            {
                return _createDocument(cancellationToken);
            }


        public sealed override void Initialize(AnalysisContext context)
        {
            context.EnableConcurrentExecution();
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.RegisterCodeBlockAction(AnalyzeCodeBlock);
        }

        private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
        {
            var method = context.CodeBlock as MethodDeclarationSyntax;
            if (method == null) return;

            if (method.Body != null)
            {
                foreach (var statement in method.Body.Statements.OfType<LocalDeclarationStatementSyntax>())
                {

                    var diagnostic = AnalyzeStatement(context.SemanticModel, statement);
                    if (diagnostic != null)
                    {
                        context.ReportDiagnostic(diagnostic);
                    }
                }
                return;
            }
        }

        protected virtual Diagnostic AnalyzeStatement(SemanticModel semanticModel, LocalDeclarationStatementSyntax statement)
        {
            var symbolInfo = semanticModel.GetSymbolInfo(statement.Declaration.Type);
            if (symbolInfo.Symbol?.Name != nameof(Action)) return null;

            foreach (var variable in statement.Declaration.Variables)
            {
                if (variable.Initializer == null) continue;

                if (!(variable.Initializer.Value is ParenthesizedLambdaExpressionSyntax lambda)) continue;

                if (lambda.AsyncKeyword.IsKind(SyntaxKind.AsyncKeyword))
                {
                    return Diagnostic.Create(descriptor: Rule, location: statement.GetLocation());
                }
            }

            return null;
        }
        public override Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            return Task.CompletedTask;
        }


        public sealed override FixAllProvider GetFixAllProvider()
        {
            return null;
        }

        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            // Find the type declaration identified by the diagnostic.
            var methodDeclaration = root.FindToken(diagnosticSpan.Start).Parent.FirstAncestorOrSelf<MethodDeclarationSyntax>();

            // Register a code action that will invoke the fix.
            context.RegisterCodeFix(
                new AsyncVoidCodeAction("Async methods should not return void",
                                        c => VoidToTaskAsync(context.Document, methodDeclaration, c)),
                diagnostic);
        }

        private async Task<Document> VoidToTaskAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
        {
            // The Task object must be parsed from a string using the Syntax Factory
            var newType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task").WithAdditionalAnnotations(Simplifier.Annotation).WithTrailingTrivia(methodDeclaration.ReturnType.GetTrailingTrivia());

            var newMethodDeclaration = methodDeclaration.WithReturnType(newType);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            var newRoot = oldRoot.ReplaceNode(methodDeclaration, newMethodDeclaration);
            var newDocument = document.WithSyntaxRoot(newRoot);

            // Return document with transformed tree.
            return newDocument;
        }

            protected override Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
            {
                return _createDocument(cancellationToken);
            }

        //No diagnostics expected to show up
        [TestMethod]
        [DynamicData(nameof(GetValidData), DynamicDataSourceType.Method)]
        public async Task CodeWithoutDiagnostics(string input)
        {
            var source = File.ReadAllText(input);
            await VerifyCS.VerifyAnalyzerAsync(source);
        }

        //Diagnostic and CodeFix both triggered and checked for
        [TestMethod]
        [DynamicData(nameof(GetInvalidData), DynamicDataSourceType.Method)]
        public async Task CodeThatRequireFix(string inputFile, int line, int col, string value, string expectedCodeFixFile)
        {
            var inputSource = File.ReadAllText(inputFile);
            var expectedCodeFixSource = File.ReadAllText(expectedCodeFixFile);
            var rule = VerifyCS.Diagnostic(AsyncVoidAnalyzer.DiagnosticId);
            var expected = rule.WithLocation(line, col).WithArguments(value.Trim());
            await VerifyCS.VerifyCodeFixAsync(inputSource, expected, expectedCodeFixSource);
        }


        public override void Initialize(AnalysisContext context)
        {
            context.EnableConcurrentExecution();
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze);

            context.RegisterSymbolAction(AnalyzeMethod, SymbolKind.Method);
        }

        private static void AnalyzeMethod(SymbolAnalysisContext symbolAnalysisContext)
        {
            var symbol = (IMethodSymbol)symbolAnalysisContext.Symbol;

            if (symbol.IsAsync && symbol.ReturnsVoid)
            {
                // This symbol is a method symbol and will have only one item in Locations property.
                var location = symbol.Locations[0]; 
                var diagnostic = Diagnostic.Create(DiagnosticDescriptors.AsyncVoidReturnType, location);
                symbolAnalysisContext.ReportDiagnostic(diagnostic);
            }
        }

Microsoft.Azure.Functions.Worker.Sdk.Analyzers.AsyncVoidAnalyzer : DiagnosticAnalyzer

Constructors :

public AsyncVoidAnalyzer()

Methods :

public ImmutableArray<DiagnosticDescriptor> get_SupportedDiagnostics()
public Void Initialize(AnalysisContext context = )
public Boolean Equals(Object obj = )
public Int32 GetHashCode()
public String ToString()
public Type GetType()