예제 #1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(
                    root,
                    context.Span,
                    out SyntaxNode node,
                    predicate: f => f.IsKind(
                        SyntaxKind.TrueLiteralExpression,
                        SyntaxKind.EqualsExpression,
                        SyntaxKind.NotEqualsExpression,
                        SyntaxKind.LogicalAndExpression,
                        SyntaxKind.LogicalOrExpression)))
            {
                return;
            }

            switch (node.Kind())
            {
            case SyntaxKind.TrueLiteralExpression:
            {
                RegisterCodeFix(
                    context,
                    node.ToString(),
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (ForStatementSyntax)node.Parent,
                                   cancellationToken));
                    });

                break;
            }

            case SyntaxKind.EqualsExpression:
            case SyntaxKind.NotEqualsExpression:
            case SyntaxKind.LogicalAndExpression:
            case SyntaxKind.LogicalOrExpression:
            {
                var binaryExpression = (BinaryExpressionSyntax)node;

                TextSpan span = RemoveRedundantBooleanLiteralAnalysis.GetSpanToRemove(binaryExpression, binaryExpression.Left, binaryExpression.Right);

                RegisterCodeFix(
                    context,
                    binaryExpression.ToString(span),
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   binaryExpression,
                                   cancellationToken));
                    });

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

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

            Debug.Assert(literalExpression != null, $"{nameof(literalExpression)} is null");

            if (literalExpression == null)
            {
                return;
            }

            Debug.Assert(literalExpression.IsBooleanLiteralExpression(), literalExpression.Kind().ToString());

            if (!literalExpression.IsBooleanLiteralExpression())
            {
                return;
            }

            SyntaxNode parent = literalExpression.Parent;

            switch (parent?.Kind())
            {
            case SyntaxKind.ForStatement:
            {
                RegisterCodeFix(
                    context,
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (ForStatementSyntax)parent,
                                   cancellationToken));
                    });

                break;
            }

            case SyntaxKind.LogicalAndExpression:
            case SyntaxKind.LogicalOrExpression:
            case SyntaxKind.EqualsExpression:
            case SyntaxKind.NotEqualsExpression:
            {
                RegisterCodeFix(
                    context,
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (BinaryExpressionSyntax)parent,
                                   cancellationToken));
                    });

                break;
            }
            }
        }
예제 #3
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            SyntaxNode node = root
                              .FindNode(context.Span, getInnermostNodeForTie: true)?
                              .FirstAncestorOrSelf(f => f.IsKind(
                                                       SyntaxKind.TrueLiteralExpression,
                                                       SyntaxKind.EqualsExpression,
                                                       SyntaxKind.NotEqualsExpression,
                                                       SyntaxKind.LogicalAndExpression,
                                                       SyntaxKind.LogicalOrExpression));

            Debug.Assert(node != null, $"{nameof(node)} is null");

            if (node == null)
            {
                return;
            }

            switch (node.Kind())
            {
            case SyntaxKind.TrueLiteralExpression:
            {
                RegisterCodeFix(
                    context,
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (ForStatementSyntax)node.Parent,
                                   cancellationToken));
                    });

                break;
            }

            case SyntaxKind.EqualsExpression:
            case SyntaxKind.NotEqualsExpression:
            case SyntaxKind.LogicalAndExpression:
            case SyntaxKind.LogicalOrExpression:
            {
                RegisterCodeFix(
                    context,
                    cancellationToken =>
                    {
                        return(RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(
                                   context.Document,
                                   (BinaryExpressionSyntax)node,
                                   cancellationToken));
                    });

                break;
            }
            }
        }
 private static void AnalyzeLogicalAndLogicalOr(
     SyntaxNodeAnalysisContext context,
     BinaryExpressionSyntax binaryExpression,
     ExpressionSyntax left,
     ExpressionSyntax right,
     SyntaxKind kind)
 {
     if (left.IsKind(kind) ||
         right.IsKind(kind))
     {
         RemoveRedundantBooleanLiteralRefactoring.ReportDiagnostic(context, binaryExpression, left, right);
     }
 }
예제 #5
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

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

            if (binaryExpression == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.RemoveRedundantBooleanLiteral:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove redundant boolean literal",
                        cancellationToken => RemoveRedundantBooleanLiteralRefactoring.RefactorAsync(context.Document, binaryExpression, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);

                    break;
                }

                case DiagnosticIdentifiers.SimplifyBooleanComparison:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Simplify boolean comparison",
                        cancellationToken => SimplifyBooleanComparisonRefactoring.RefactorAsync(context.Document, binaryExpression, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);

                    break;
                }

                case DiagnosticIdentifiers.UseAnyMethodInsteadOfCountMethod:
                {
                    UseAnyMethodInsteadOfCountMethodCodeFix.Register(context, diagnostic, binaryExpression);
                    break;
                }
                }
            }
        }
예제 #6
0
        private static void AnalyzeEqualsNotEquals(
            SyntaxNodeAnalysisContext context,
            BinaryExpressionSyntax binaryExpression,
            ExpressionSyntax left,
            ExpressionSyntax right,
            SyntaxKind kind,
            SyntaxKind kind2)
        {
            SyntaxKind leftKind = left.Kind();

            if (leftKind == kind)
            {
                if (IsBooleanExpressionButNotBooleanLiteral(context, right))
                {
                    SimplifyBooleanComparisonRefactoring.ReportDiagnostic(context, binaryExpression);
                }
            }
            else if (leftKind == kind2)
            {
                if (IsBooleanExpressionButNotBooleanLiteral(context, right))
                {
                    RemoveRedundantBooleanLiteralRefactoring.ReportDiagnostic(context, binaryExpression, left);
                }
            }
            else
            {
                SyntaxKind rightKind = right.Kind();

                if (rightKind == kind)
                {
                    if (IsBooleanExpressionButNotBooleanLiteral(context, left))
                    {
                        SimplifyBooleanComparisonRefactoring.ReportDiagnostic(context, binaryExpression);
                    }
                }
                else if (rightKind == kind2)
                {
                    if (IsBooleanExpressionButNotBooleanLiteral(context, left))
                    {
                        RemoveRedundantBooleanLiteralRefactoring.ReportDiagnostic(context, binaryExpression, right);
                    }
                }
            }
        }
        private static void AnalyzeEqualsNotEquals(
            SyntaxNodeAnalysisContext context,
            BinaryExpressionSyntax binaryExpression,
            ExpressionSyntax left,
            ExpressionSyntax right,
            SyntaxKind kind,
            SyntaxKind kind2)
        {
            SyntaxKind leftKind = left.Kind();

            if (leftKind == kind)
            {
                switch (AnalyzeExpression(right, context.SemanticModel, context.CancellationToken))
                {
                case AnalysisResult.Boolean:
                {
                    SimplifyBooleanComparisonRefactoring.ReportDiagnostic(context, binaryExpression, left, right, fadeOut: true);
                    break;
                }

                case AnalysisResult.LogicalNotWithNullableBoolean:
                {
                    SimplifyBooleanComparisonRefactoring.ReportDiagnostic(context, binaryExpression, left, right, fadeOut: false);
                    break;
                }
                }
            }
            else if (leftKind == kind2)
            {
                switch (AnalyzeExpression(right, context.SemanticModel, context.CancellationToken))
                {
                case AnalysisResult.Boolean:
                {
                    RemoveRedundantBooleanLiteralRefactoring.
                    ReportDiagnostic(context, binaryExpression, left, right, left);
                    break;
                }

                case AnalysisResult.LogicalNotWithNullableBoolean:
                {
                    SimplifyBooleanComparisonRefactoring.ReportDiagnostic(context, binaryExpression, left, right, fadeOut: false);
                    break;
                }
                }
            }
            else
            {
                SyntaxKind rightKind = right.Kind();

                if (rightKind == kind)
                {
                    switch (AnalyzeExpression(left, context.SemanticModel, context.CancellationToken))
                    {
                    case AnalysisResult.Boolean:
                    {
                        SimplifyBooleanComparisonRefactoring.ReportDiagnostic(context, binaryExpression, left, right, fadeOut: true);
                        break;
                    }

                    case AnalysisResult.LogicalNotWithNullableBoolean:
                    {
                        SimplifyBooleanComparisonRefactoring.ReportDiagnostic(context, binaryExpression, left, right, fadeOut: false);
                        break;
                    }
                    }
                }
                else if (rightKind == kind2)
                {
                    switch (AnalyzeExpression(left, context.SemanticModel, context.CancellationToken))
                    {
                    case AnalysisResult.Boolean:
                    {
                        RemoveRedundantBooleanLiteralRefactoring.ReportDiagnostic(context, binaryExpression, left, right, right);
                        break;
                    }

                    case AnalysisResult.LogicalNotWithNullableBoolean:
                    {
                        SimplifyBooleanComparisonRefactoring.ReportDiagnostic(context, binaryExpression, left, right, fadeOut: false);
                        break;
                    }
                    }
                }
            }
        }