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;
            }

            CodeAction codeAction = CodeAction.Create(
                "Use while to create an infinite loop",
                cancellationToken =>
            {
                return(ReplaceDoStatementWithWhileStatementRefactoring.RefactorAsync(
                           context.Document,
                           doStatement,
                           cancellationToken));
            },
                DiagnosticIdentifiers.AvoidUsageOfDoStatementToCreateInfiniteLoop + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        private void AnalyzeDoStatement(SyntaxNodeAnalysisContext context)
        {
            var doStatement = (DoStatementSyntax)context.Node;

            if (ReplaceDoStatementWithWhileStatementRefactoring.CanRefactor(doStatement))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AvoidUsageOfDoStatementToCreateInfiniteLoop,
                    doStatement.DoKeyword);
            }

            AddEmptyLineAfterLastStatementInDoStatementRefactoring.Analyze(context, doStatement);
        }
        private void AnalyzeDoStatement(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var doStatement = (DoStatementSyntax)context.Node;

            if (ReplaceDoStatementWithWhileStatementRefactoring.CanRefactor(doStatement))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AvoidUsageOfDoStatementToCreateInfiniteLoop,
                    doStatement.DoKeyword.GetLocation());
            }

            AnalyzeAddEmptyLineAfterLastStatementInDoStatement(context, doStatement);
        }
Beispiel #4
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out DoStatementSyntax doStatement))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Use while to create an infinite loop",
                cancellationToken =>
            {
                return(ReplaceDoStatementWithWhileStatementRefactoring.RefactorAsync(
                           context.Document,
                           doStatement,
                           cancellationToken));
            },
                GetEquivalenceKey(DiagnosticIdentifiers.AvoidUsageOfDoStatementToCreateInfiniteLoop));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }