private static void AnalyzeExpression(
            SyntaxNodeAnalysisContext context,
            ExpressionSyntax expression,
            SyntaxToken openParenToken,
            SyntaxToken closeParenToken)
        {
            if (expression?.IsKind(SyntaxKind.ParenthesizedExpression) == true)
            {
                var parenthesizedExpression = (ParenthesizedExpressionSyntax)expression;

                if (openParenToken.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLine()) &&
                    closeParenToken.LeadingTrivia.All(f => f.IsWhitespaceOrEndOfLine()) &&
                    parenthesizedExpression.GetLeadingTrivia().All(f => f.IsWhitespaceOrEndOfLine()) &&
                    parenthesizedExpression.GetTrailingTrivia().All(f => f.IsWhitespaceOrEndOfLine()))
                {
                    Diagnostic diagnostic = Diagnostic.Create(
                        DiagnosticDescriptors.RemoveRedundantParentheses,
                        parenthesizedExpression.OpenParenToken.GetLocation(),
                        additionalLocations: new Location[] { parenthesizedExpression.CloseParenToken.GetLocation() });

                    context.ReportDiagnostic(diagnostic);

                    DiagnosticHelper.FadeOutToken(context, parenthesizedExpression.OpenParenToken, DiagnosticDescriptors.RemoveRedundantParenthesesFadeOut);
                    DiagnosticHelper.FadeOutToken(context, parenthesizedExpression.CloseParenToken, DiagnosticDescriptors.RemoveRedundantParenthesesFadeOut);
                }
            }
        }
Beispiel #2
0
        private static void FadeOut(SyntaxNodeAnalysisContext context, BlockSyntax block)
        {
            DiagnosticDescriptor descriptor = DiagnosticDescriptors.SimplifyLambdaExpressionFadeOut;

            DiagnosticHelper.FadeOutBraces(context, block, descriptor);

            if (block.Statements[0].IsKind(SyntaxKind.ReturnStatement))
            {
                DiagnosticHelper.FadeOutToken(context, ((ReturnStatementSyntax)block.Statements[0]).ReturnKeyword, descriptor);
            }
        }
Beispiel #3
0
        public static void Analyze(SyntaxNodeAnalysisContext context)
        {
            var returnStatement = (ReturnStatementSyntax)context.Node;

            if (returnStatement.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.GetSymbolInfo(returnStatement.Expression, context.CancellationToken).Symbol;

            if (symbol?.Kind != SymbolKind.Local)
            {
                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.IsWhitespaceOrEndOfLine()))
                {
                    context.ReportDiagnostic(
                        DiagnosticDescriptors.MergeLocalDeclarationWithReturnStatement,
                        Location.Create(context.Node.SyntaxTree, span));
                }

                DiagnosticHelper.FadeOutNode(context, localDeclaration.Declaration.Type, DiagnosticDescriptors.MergeLocalDeclarationWithReturnStatementFadeOut);
                DiagnosticHelper.FadeOutToken(context, declarator.Identifier, DiagnosticDescriptors.MergeLocalDeclarationWithReturnStatementFadeOut);
                DiagnosticHelper.FadeOutToken(context, declarator.Initializer.EqualsToken, DiagnosticDescriptors.MergeLocalDeclarationWithReturnStatementFadeOut);
                DiagnosticHelper.FadeOutToken(context, localDeclaration.SemicolonToken, DiagnosticDescriptors.MergeLocalDeclarationWithReturnStatementFadeOut);
                DiagnosticHelper.FadeOutNode(context, returnStatement.Expression, DiagnosticDescriptors.MergeLocalDeclarationWithReturnStatementFadeOut);
            }
        }
Beispiel #4
0
        private static void FadeOut(
            SyntaxNodeAnalysisContext context,
            IfStatementSyntax ifStatement,
            IfStatementSyntax ifStatement2)
        {
            DiagnosticHelper.FadeOutToken(context, ifStatement2.IfKeyword, DiagnosticDescriptors.MergeIfStatementWithContainedIfStatementFadeOut);
            DiagnosticHelper.FadeOutToken(context, ifStatement2.OpenParenToken, DiagnosticDescriptors.MergeIfStatementWithContainedIfStatementFadeOut);
            DiagnosticHelper.FadeOutToken(context, ifStatement2.CloseParenToken, DiagnosticDescriptors.MergeIfStatementWithContainedIfStatementFadeOut);

            if (ifStatement.Statement.IsKind(SyntaxKind.Block) &&
                ifStatement2.Statement.IsKind(SyntaxKind.Block))
            {
                DiagnosticHelper.FadeOutBraces(context, (BlockSyntax)ifStatement2.Statement, DiagnosticDescriptors.MergeIfStatementWithContainedIfStatementFadeOut);
            }
        }
        private static void FadeOut(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement, ReturnStatementSyntax returnStatement)
        {
            DiagnosticHelper.FadeOutToken(context, ifStatement.IfKeyword, _fadeOutDescriptor);
            DiagnosticHelper.FadeOutToken(context, ifStatement.OpenParenToken, _fadeOutDescriptor);
            DiagnosticHelper.FadeOutToken(context, ifStatement.CloseParenToken, _fadeOutDescriptor);
            DiagnosticHelper.FadeOutNode(context, ifStatement.Statement, _fadeOutDescriptor);

            if (ifStatement.Else != null)
            {
                DiagnosticHelper.FadeOutNode(context, ifStatement.Else, _fadeOutDescriptor);
            }
            else
            {
                DiagnosticHelper.FadeOutNode(context, returnStatement, _fadeOutDescriptor);
            }
        }
Beispiel #6
0
        private static bool AnalyzeBlock(SyntaxNodeAnalysisContext context, BlockSyntax block, bool checkTrivia = true)
        {
            if (block == null)
            {
                return(false);
            }

            SyntaxList <StatementSyntax> statements = block.Statements;

            if (statements.Count == 0)
            {
                return(false);
            }

            var returnStatement = statements[0] as ReturnStatementSyntax;

            if (returnStatement == null)
            {
                return(false);
            }

            if (returnStatement.Expression == null)
            {
                return(false);
            }

            if (checkTrivia && block
                .DescendantTrivia(descendIntoTrivia: true)
                .Any(f => !f.IsWhitespaceOrEndOfLine()))
            {
                return(false);
            }

            if (!returnStatement.IsSingleline())
            {
                return(false);
            }

            context.ReportDiagnostic(
                DiagnosticDescriptors.UseExpressionBodiedMember,
                block.GetLocation());

            DiagnosticHelper.FadeOutToken(context, returnStatement.ReturnKeyword, DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut);
            DiagnosticHelper.FadeOutBraces(context, block, DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut);

            return(true);
        }
Beispiel #7
0
        private static void AnalyzeAccessorList(SyntaxNodeAnalysisContext context, AccessorListSyntax accessorList)
        {
            if (accessorList == null)
            {
                return;
            }

            SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors;

            if (accessors.Count != 1)
            {
                return;
            }

            AccessorDeclarationSyntax accessor = accessors[0];

            if (accessor.Body == null)
            {
                return;
            }

            if (!accessor.IsKind(SyntaxKind.GetAccessorDeclaration))
            {
                return;
            }

            if (accessor.AttributeLists.Count != 0)
            {
                return;
            }

            if (accessor.Body
                .DescendantTrivia(descendIntoTrivia: true)
                .Any(f => !f.IsWhitespaceOrEndOfLine()))
            {
                return;
            }

            if (AnalyzeBlock(context, accessor.Body, checkTrivia: false))
            {
                DiagnosticHelper.FadeOutToken(context, accessor.Keyword, DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut);
                DiagnosticHelper.FadeOutBraces(context, accessorList, DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut);
            }
        }