Exemplo n.º 1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            DoStatementSyntax doStatement = root
                                            .FindNode(context.Span, getInnermostNodeForTie: true)?
                                            .FirstAncestorOrSelf <DoStatementSyntax>();

            if (doStatement == null)
            {
                return;
            }

            TextSpan span = TextSpan.FromBounds(
                doStatement.Statement.Span.End,
                doStatement.Span.End);

            if (root
                .DescendantTrivia(span)
                .All(f => f.IsWhitespaceOrEndOfLine()))
            {
                CodeAction codeAction = CodeAction.Create(
                    "Use while statement to create an infinite loop",
                    cancellationToken =>
                {
                    return(DoStatementRefactoring.ConvertToWhileStatementAsync(
                               context.Document,
                               doStatement,
                               cancellationToken));
                },
                    DiagnosticIdentifiers.AvoidUsageOfDoStatementToCreateInfiniteLoop + EquivalenceKeySuffix);

                context.RegisterCodeFix(codeAction, context.Diagnostics);
            }
        }
        private void AnalyzeDoStatement(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var doStatement = (DoStatementSyntax)context.Node;

            if (DoStatementRefactoring.CanConvertToWhileStatement(doStatement))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AvoidUsageOfDoStatementToCreateInfiniteLoop,
                    doStatement.DoKeyword.GetLocation());
            }
        }