private static void FadeOut(SyntaxNodeAnalysisContext context, PropertyDeclarationSyntax property)
        {
            DiagnosticDescriptor descriptor = DiagnosticDescriptors.ReplacePropertyWithAutoImplementedPropertyFadeOut;

            if (property.ExpressionBody != null)
            {
                context.FadeOutNode(descriptor, property.ExpressionBody);
            }
            else
            {
                AccessorDeclarationSyntax getter = property.Getter();

                if (getter != null)
                {
                    context.FadeOutNode(descriptor, getter.Body);
                }

                AccessorDeclarationSyntax setter = property.Setter();

                if (setter != null)
                {
                    context.FadeOutNode(descriptor, setter.Body);
                }
            }
        }
        private static void SimplifyBooleanComparisonFadeOut(
            SyntaxNodeAnalysisContext context,
            BinaryExpressionSyntax binaryExpression)
        {
            DiagnosticDescriptor descriptor = DiagnosticDescriptors.SimplifyBooleanComparisonFadeOut;

            context.FadeOutToken(descriptor, binaryExpression.OperatorToken);

            ExpressionSyntax left  = binaryExpression.Left;
            ExpressionSyntax right = binaryExpression.Right;

            if (binaryExpression.IsKind(SyntaxKind.EqualsExpression))
            {
                if (left.IsKind(SyntaxKind.FalseLiteralExpression))
                {
                    context.FadeOutNode(descriptor, left);

                    if (right.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        context.FadeOutToken(descriptor, ((PrefixUnaryExpressionSyntax)right).OperatorToken);
                    }
                }
                else if (right.IsKind(SyntaxKind.FalseLiteralExpression))
                {
                    context.FadeOutNode(descriptor, right);

                    if (left.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        context.FadeOutToken(descriptor, ((PrefixUnaryExpressionSyntax)left).OperatorToken);
                    }
                }
            }
            else if (binaryExpression.IsKind(SyntaxKind.NotEqualsExpression))
            {
                if (left.IsKind(SyntaxKind.TrueLiteralExpression))
                {
                    context.FadeOutNode(descriptor, left);

                    if (right.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        context.FadeOutToken(descriptor, ((PrefixUnaryExpressionSyntax)right).OperatorToken);
                    }
                }
                else if (right.IsKind(SyntaxKind.TrueLiteralExpression))
                {
                    context.FadeOutNode(descriptor, right);

                    if (left.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        context.FadeOutToken(descriptor, ((PrefixUnaryExpressionSyntax)left).OperatorToken);
                    }
                }
            }
        }
        public static void Analyze(SyntaxNodeAnalysisContext context)
        {
            var returnStatement         = (ReturnStatementSyntax)context.Node;
            ExpressionSyntax expression = returnStatement.Expression;

            if (expression?.IsKind(SyntaxKind.IdentifierName) != true)
            {
                return;
            }

            LocalDeclarationStatementSyntax localDeclaration = GetLocalDeclaration(returnStatement);

            if (localDeclaration == null)
            {
                return;
            }

            VariableDeclaratorSyntax declarator = GetVariableDeclarator(localDeclaration);

            if (declarator == null)
            {
                return;
            }

            ISymbol symbol = context.SemanticModel.GetSymbol(expression, context.CancellationToken);

            if (symbol?.IsLocal() != true)
            {
                return;
            }

            ISymbol symbol2 = context.SemanticModel.GetDeclaredSymbol(declarator, context.CancellationToken);

            if (symbol.Equals(symbol2))
            {
                TextSpan span = TextSpan.FromBounds(localDeclaration.Span.Start, returnStatement.Span.End);

                if (returnStatement.Parent
                    .DescendantTrivia(span, descendIntoTrivia: false)
                    .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                {
                    context.ReportDiagnostic(
                        DiagnosticDescriptors.MergeLocalDeclarationWithReturnStatement,
                        Location.Create(context.Node.SyntaxTree, span));
                }

                context.FadeOutNode(FadeOutDescriptor, localDeclaration.Declaration.Type);
                context.FadeOutToken(FadeOutDescriptor, declarator.Identifier);
                context.FadeOutToken(FadeOutDescriptor, declarator.Initializer.EqualsToken);
                context.FadeOutToken(FadeOutDescriptor, localDeclaration.SemicolonToken);
                context.FadeOutNode(FadeOutDescriptor, expression);
            }
        }
        private static void FadeOut(
            SyntaxNodeAnalysisContext context,
            BinaryExpressionSyntax binaryExpression)
        {
            context.FadeOutToken(FadeOutDescriptor, binaryExpression.OperatorToken);

            ExpressionSyntax left  = binaryExpression.Left;
            ExpressionSyntax right = binaryExpression.Right;

            if (binaryExpression.IsKind(SyntaxKind.EqualsExpression))
            {
                if (left.IsKind(SyntaxKind.FalseLiteralExpression))
                {
                    context.FadeOutNode(FadeOutDescriptor, left);

                    if (right.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        context.FadeOutToken(FadeOutDescriptor, ((PrefixUnaryExpressionSyntax)right).OperatorToken);
                    }
                }
                else if (right.IsKind(SyntaxKind.FalseLiteralExpression))
                {
                    context.FadeOutNode(FadeOutDescriptor, right);

                    if (left.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        context.FadeOutToken(FadeOutDescriptor, ((PrefixUnaryExpressionSyntax)left).OperatorToken);
                    }
                }
            }
            else if (binaryExpression.IsKind(SyntaxKind.NotEqualsExpression))
            {
                if (left.IsKind(SyntaxKind.TrueLiteralExpression))
                {
                    context.FadeOutNode(FadeOutDescriptor, left);

                    if (right.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        context.FadeOutToken(FadeOutDescriptor, ((PrefixUnaryExpressionSyntax)right).OperatorToken);
                    }
                }
                else if (right.IsKind(SyntaxKind.TrueLiteralExpression))
                {
                    context.FadeOutNode(FadeOutDescriptor, right);

                    if (left.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        context.FadeOutToken(FadeOutDescriptor, ((PrefixUnaryExpressionSyntax)left).OperatorToken);
                    }
                }
            }
        }
Exemple #5
0
        private static void FadeOut(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement, ReturnStatementSyntax returnStatement)
        {
            context.FadeOutToken(_fadeOutDescriptor, ifStatement.IfKeyword);
            context.FadeOutToken(_fadeOutDescriptor, ifStatement.OpenParenToken);
            context.FadeOutToken(_fadeOutDescriptor, ifStatement.CloseParenToken);
            context.FadeOutNode(_fadeOutDescriptor, ifStatement.Statement);

            if (ifStatement.Else != null)
            {
                context.FadeOutNode(_fadeOutDescriptor, ifStatement.Else);
            }
            else
            {
                context.FadeOutNode(_fadeOutDescriptor, returnStatement);
            }
        }
        private void AnalyzeForStatement(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var forStatement = (ForStatementSyntax)context.Node;

            if (forStatement.Declaration == null &&
                forStatement.Condition == null &&
                forStatement.Incrementors.Count == 0 &&
                forStatement.Initializers.Count == 0 &&
                !forStatement.ContainsDirectives(
                    TextSpan.FromBounds(
                        forStatement.OpenParenToken.Span.End,
                        forStatement.CloseParenToken.Span.Start)))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AvoidUsageOfForStatementToCreateInfiniteLoop,
                    forStatement.ForKeyword.GetLocation());
            }

            if (forStatement.Condition?.IsKind(SyntaxKind.TrueLiteralExpression) == true)
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.RemoveRedundantBooleanLiteral,
                    forStatement.Condition.GetLocation());

                context.FadeOutNode(
                    DiagnosticDescriptors.RemoveRedundantBooleanLiteralFadeOut,
                    forStatement.Condition);
            }
        }
Exemple #7
0
        private static void Analyze(
            SyntaxNodeAnalysisContext context,
            ParameterListSyntax parameterList,
            SeparatedSyntaxList <ParameterSyntax> parameters)
        {
            if (parameterList
                .DescendantTrivia(parameterList.Span)
                .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.SimplifyLambdaExpressionParameterList,
                    parameterList.GetLocation());

                foreach (ParameterSyntax parameter in parameters)
                {
                    if (parameter.Type != null)
                    {
                        context.FadeOutNode(FadeOutDescriptor, parameter.Type);
                    }
                }

                if (parameters.Count == 1)
                {
                    context.FadeOutToken(FadeOutDescriptor, parameterList.OpenParenToken);
                    context.FadeOutToken(FadeOutDescriptor, parameterList.CloseParenToken);
                }
            }
        }
        private void AnalyzeSimpleAssignment(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var assignment = (AssignmentExpressionSyntax)context.Node;

            ExpressionSyntax left  = assignment.Left;
            ExpressionSyntax right = assignment.Right;

            if (left?.IsMissing == false &&
                right?.IsMissing == false)
            {
                if (!assignment.IsParentKind(SyntaxKind.ObjectInitializerExpression) &&
                    SupportsCompoundAssignment(right))
                {
                    var binaryExpression         = (BinaryExpressionSyntax)right;
                    ExpressionSyntax binaryLeft  = binaryExpression.Left;
                    ExpressionSyntax binaryRight = binaryExpression.Right;

                    if (binaryLeft?.IsMissing == false &&
                        binaryRight?.IsMissing == false &&
                        left.IsEquivalentTo(binaryLeft, topLevel: false) &&
                        ContainsOnlyWhitespaceOrEndOfLineTrivia(assignment))
                    {
                        context.ReportDiagnostic(
                            DiagnosticDescriptors.SimplifyAssignmentExpression,
                            assignment.GetLocation());

                        context.FadeOutNode(DiagnosticDescriptors.SimplifyAssignmentExpressionFadeOut, binaryLeft);
                    }
                }

                if (right.IsKind(SyntaxKind.AddExpression, SyntaxKind.SubtractExpression))
                {
                    var binaryExpression         = (BinaryExpressionSyntax)right;
                    ExpressionSyntax binaryLeft  = binaryExpression.Left;
                    ExpressionSyntax binaryRight = binaryExpression.Right;

                    if (binaryLeft?.IsMissing == false &&
                        binaryRight?.IsNumericLiteralExpression(1) == true)
                    {
                        ITypeSymbol typeSymbol = context.SemanticModel.GetTypeInfo(left, context.CancellationToken).Type;

                        if (typeSymbol?.SupportsPrefixOrPostfixUnaryOperator() == true &&
                            left.IsEquivalentTo(binaryLeft, topLevel: false) &&
                            !assignment.SpanContainsDirectives())
                        {
                            context.ReportDiagnostic(
                                DiagnosticDescriptors.UsePostfixUnaryOperatorInsteadOfAssignment,
                                assignment.GetLocation(),
                                UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.GetOperatorText(assignment));
                        }
                    }
                }
            }
        }
        private static void RemoveRedundantBooleanLiteralFadeOut(
            SyntaxNodeAnalysisContext context,
            BinaryExpressionSyntax binaryExpression)
        {
            DiagnosticDescriptor descriptor = DiagnosticDescriptors.RemoveRedundantBooleanLiteralFadeOut;

            context.FadeOutToken(descriptor, binaryExpression.OperatorToken);

            ExpressionSyntax left  = binaryExpression.Left;
            ExpressionSyntax right = binaryExpression.Right;

            switch (binaryExpression.Kind())
            {
            case SyntaxKind.EqualsExpression:
            case SyntaxKind.LogicalAndExpression:
            {
                if (left.IsKind(SyntaxKind.TrueLiteralExpression))
                {
                    context.FadeOutNode(descriptor, left);
                }
                else if (right.IsKind(SyntaxKind.TrueLiteralExpression))
                {
                    context.FadeOutNode(descriptor, right);
                }

                break;
            }

            case SyntaxKind.NotEqualsExpression:
            case SyntaxKind.LogicalOrExpression:
            {
                if (left.IsKind(SyntaxKind.FalseLiteralExpression))
                {
                    context.FadeOutNode(descriptor, left);
                }
                else if (right.IsKind(SyntaxKind.FalseLiteralExpression))
                {
                    context.FadeOutNode(descriptor, right);
                }

                break;
            }
            }
        }
        public static void Analyze(SyntaxNodeAnalysisContext context, AssignmentExpressionSyntax assignment)
        {
            ExpressionSyntax right = assignment.Right;

            if (right?.IsKind(SyntaxKind.ObjectCreationExpression) == true)
            {
                var objectCreation = (ObjectCreationExpressionSyntax)right;

                SemanticModel     semanticModel     = context.SemanticModel;
                CancellationToken cancellationToken = context.CancellationToken;

                ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(objectCreation, cancellationToken);

                if (typeSymbol != null &&
                    Symbol.IsEventHandlerOrConstructedFromEventHandlerOfT(typeSymbol, semanticModel))
                {
                    ArgumentListSyntax argumentList = objectCreation.ArgumentList;

                    if (argumentList != null)
                    {
                        SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

                        if (arguments.Count == 1)
                        {
                            ArgumentSyntax argument = arguments.First();

                            ExpressionSyntax expression = argument.Expression;

                            if (expression != null)
                            {
                                IMethodSymbol methodSymbol = semanticModel.GetMethodSymbol(expression, cancellationToken);

                                if (methodSymbol != null)
                                {
                                    ExpressionSyntax left = assignment.Left;

                                    if (left?.IsMissing == false &&
                                        semanticModel.GetSymbol(left, cancellationToken)?.IsEvent() == true &&
                                        !objectCreation.SpanContainsDirectives())
                                    {
                                        context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantDelegateCreation, right.GetLocation());

                                        context.FadeOutToken(DiagnosticDescriptors.RemoveRedundantDelegateCreationFadeOut, objectCreation.NewKeyword);
                                        context.FadeOutNode(DiagnosticDescriptors.RemoveRedundantDelegateCreationFadeOut, objectCreation.Type);
                                        context.FadeOutParentheses(DiagnosticDescriptors.RemoveRedundantDelegateCreationFadeOut, objectCreation.ArgumentList);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        private void AnalyzeForStatement(SyntaxNodeAnalysisContext context)
        {
            var forStatement = (ForStatementSyntax)context.Node;

            AvoidUsageOfForStatementToCreateInfiniteLoopRefactoring.Analyze(context, forStatement);

            ExpressionSyntax condition = forStatement.Condition;

            if (condition?.IsKind(SyntaxKind.TrueLiteralExpression) == true)
            {
                context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantBooleanLiteral, condition.GetLocation());
                context.FadeOutNode(DiagnosticDescriptors.RemoveRedundantBooleanLiteralFadeOut, condition);
            }
        }
        private void AnalyzeSimpleAssignment(SyntaxNodeAnalysisContext context)
        {
            var assignment = (AssignmentExpressionSyntax)context.Node;

            if (UseCompoundAssignmentRefactoring.CanRefactor(assignment))
            {
                var binaryExpression = (BinaryExpressionSyntax)assignment.Right;

                context.ReportDiagnostic(DiagnosticDescriptors.UseCompoundAssignment, assignment.GetLocation(), UseCompoundAssignmentRefactoring.GetCompoundOperatorText(binaryExpression));
                context.FadeOutNode(DiagnosticDescriptors.UseCompoundAssignmentFadeOut, binaryExpression.Left);
            }

            UsePostfixUnaryOperatorInsteadOfAssignmentRefactoring.Analyze(context, assignment);
        }
        private static void FadeOut(SyntaxNodeAnalysisContext context, ParameterListSyntax parameterList)
        {
            foreach (ParameterSyntax parameter in parameterList.Parameters)
            {
                if (parameter.Type != null)
                {
                    context.FadeOutNode(DiagnosticDescriptors.SimplifyLambdaExpressionParameterListFadeOut, parameter.Type);
                }
            }

            if (parameterList.Parameters.Count == 1)
            {
                context.FadeOutToken(DiagnosticDescriptors.SimplifyLambdaExpressionParameterListFadeOut, parameterList.OpenParenToken);
                context.FadeOutToken(DiagnosticDescriptors.SimplifyLambdaExpressionParameterListFadeOut, parameterList.CloseParenToken);
            }
        }
Exemple #14
0
        public static void Analyze(SyntaxNodeAnalysisContext context, AssignmentExpressionSyntax assignment)
        {
            switch (assignment.Kind())
            {
            case SyntaxKind.AddAssignmentExpression:
            case SyntaxKind.SubtractAssignmentExpression:
            {
                ExpressionSyntax left  = assignment.Left;
                ExpressionSyntax right = assignment.Right;

                if (left?.IsMissing == false &&
                    right?.IsNumericLiteralExpression(1) == true)
                {
                    ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(left, context.CancellationToken);

                    if (typeSymbol?.SupportsPrefixOrPostfixUnaryOperator() == true &&
                        !assignment.SpanContainsDirectives())
                    {
                        ReportDiagnostic(context, assignment);

                        SyntaxToken operatorToken = assignment.OperatorToken;

                        if (operatorToken.Span.Length == 2)
                        {
                            context.ReportDiagnostic(FadeOutDescriptor, Location.Create(assignment.SyntaxTree, new TextSpan(operatorToken.SpanStart, 1)));
                        }

                        context.FadeOutNode(FadeOutDescriptor, assignment.Right);
                    }
                }

                break;
            }

            case SyntaxKind.SimpleAssignmentExpression:
            {
                ExpressionSyntax left  = assignment.Left;
                ExpressionSyntax right = assignment.Right;

                if (left?.IsMissing == false &&
                    right?.IsMissing == false &&
                    right.IsKind(SyntaxKind.AddExpression, SyntaxKind.SubtractExpression))
                {
                    var binaryExpression         = (BinaryExpressionSyntax)right;
                    ExpressionSyntax binaryLeft  = binaryExpression.Left;
                    ExpressionSyntax binaryRight = binaryExpression.Right;

                    if (binaryLeft?.IsMissing == false &&
                        binaryRight?.IsNumericLiteralExpression(1) == true)
                    {
                        ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(left, context.CancellationToken);

                        if (typeSymbol?.SupportsPrefixOrPostfixUnaryOperator() == true &&
                            left.IsEquivalentTo(binaryLeft, topLevel: false) &&
                            !assignment.SpanContainsDirectives())
                        {
                            ReportDiagnostic(context, assignment);

                            context.FadeOutToken(FadeOutDescriptor, assignment.OperatorToken);
                            context.FadeOutNode(FadeOutDescriptor, binaryLeft);
                            context.FadeOutNode(FadeOutDescriptor, binaryRight);
                        }
                    }
                }

                break;
            }
            }
        }
Exemple #15
0
        public static void Analyze(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement)
        {
            SyntaxNode parent = ifStatement.Parent;

            if (parent?.IsKind(SyntaxKind.Block) == true)
            {
                ReturnStatementSyntax   returnStatement = GetReturnStatement(ifStatement.Statement);
                LiteralExpressionSyntax booleanLiteral  = GetBooleanLiteral(returnStatement);

                if (booleanLiteral != null)
                {
                    ReturnStatementSyntax   returnStatement2 = null;
                    LiteralExpressionSyntax booleanLiteral2  = null;
                    TextSpan         span  = ifStatement.Span;
                    ElseClauseSyntax @else = ifStatement.Else;

                    if (@else != null)
                    {
                        returnStatement2 = GetReturnStatement(@else.Statement);
                        booleanLiteral2  = GetBooleanLiteral(returnStatement2);
                    }
                    else
                    {
                        var block = (BlockSyntax)parent;
                        SyntaxList <StatementSyntax> statements = block.Statements;

                        int index = statements.IndexOf(ifStatement);

                        if (index < statements.Count - 1 &&
                            (index == 0 || !IsIfStatementWithReturnStatement(statements[index - 1])))
                        {
                            StatementSyntax nextStatement = statements[index + 1];

                            if (nextStatement.IsKind(SyntaxKind.ReturnStatement))
                            {
                                returnStatement2 = (ReturnStatementSyntax)nextStatement;
                                booleanLiteral2  = GetBooleanLiteral(returnStatement2);

                                if (booleanLiteral2 != null)
                                {
                                    span = TextSpan.FromBounds(ifStatement.SpanStart, returnStatement2.Span.End);
                                }
                            }
                        }
                    }

                    if (booleanLiteral2 != null &&
                        IsOppositeBooleanLiteral(booleanLiteral, booleanLiteral2) &&
                        parent
                        .DescendantTrivia(span)
                        .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        context.ReportDiagnostic(
                            DiagnosticDescriptors.ReplaceIfStatementWithReturnStatement,
                            Location.Create(context.Node.SyntaxTree, span));

                        context.FadeOutToken(FadeOutDescriptor, ifStatement.IfKeyword);
                        context.FadeOutToken(FadeOutDescriptor, ifStatement.OpenParenToken);
                        context.FadeOutToken(FadeOutDescriptor, ifStatement.CloseParenToken);
                        context.FadeOutNode(FadeOutDescriptor, ifStatement.Statement);

                        if (ifStatement.Else != null)
                        {
                            context.FadeOutNode(FadeOutDescriptor, @else);
                        }
                        else
                        {
                            context.FadeOutNode(FadeOutDescriptor, returnStatement2);
                        }
                    }
                }
            }
        }