Ejemplo n.º 1
0
        public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
        {
            var ifStatement = (IfStatementSyntax)context.Node;

            if (ifStatement.IsSimpleIf() &&
                !ifStatement.ContainsDiagnostics)
            {
                SyntaxList <StatementSyntax> statements;
                if (ifStatement.TryGetContainingList(out statements) &&
                    !IsPartOfLazyInitialization(ifStatement, statements))
                {
                    EqualsToNullExpression equalsToNull;
                    if (EqualsToNullExpression.TryCreate(ifStatement.Condition, out equalsToNull))
                    {
                        SimpleAssignmentStatement assignment;
                        if (SimpleAssignmentStatement.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment) &&
                            SyntaxComparer.AreEquivalent(assignment.Left, equalsToNull.Left) &&
                            assignment.Right.IsSingleLine() &&
                            !ifStatement.SpanContainsDirectives())
                        {
                            int index = statements.IndexOf(ifStatement);

                            if (index > 0)
                            {
                                StatementSyntax previousStatement = statements[index - 1];

                                if (!previousStatement.ContainsDiagnostics &&
                                    CanRefactor(previousStatement, ifStatement, equalsToNull.Left, ifStatement.Parent))
                                {
                                    context.ReportDiagnostic(DiagnosticDescriptors.UseCoalesceExpression, previousStatement);
                                }
                            }

                            if (index < statements.Count - 1)
                            {
                                StatementSyntax nextStatement = statements[index + 1];

                                if (!nextStatement.ContainsDiagnostics)
                                {
                                    MemberInvocationStatement memberInvocation;
                                    if (MemberInvocationStatement.TryCreate(nextStatement, out memberInvocation) &&
                                        SyntaxComparer.AreEquivalent(equalsToNull.Left, memberInvocation.Expression) &&
                                        !ifStatement.Parent.ContainsDirectives(TextSpan.FromBounds(ifStatement.SpanStart, nextStatement.Span.End)))
                                    {
                                        context.ReportDiagnostic(DiagnosticDescriptors.InlineLazyInitialization, ifStatement);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
 private static void Analyze(SyntaxNodeAnalysisContext context, BinaryExpressionSyntax binaryExpression)
 {
     if (!binaryExpression.ContainsDiagnostics)
     {
         EqualsToNullExpression equalsToNull;
         if (EqualsToNullExpression.TryCreate(binaryExpression, out equalsToNull) &&
             IsUnconstrainedTypeParameter(context.SemanticModel.GetTypeSymbol(equalsToNull.Left, context.CancellationToken)) &&
             !binaryExpression.SpanContainsDirectives())
         {
             context.ReportDiagnostic(DiagnosticDescriptors.UnconstrainedTypeParameterCheckedForNull, binaryExpression);
         }
     }
 }
        public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
        {
            var ifStatement = (IfStatementSyntax)context.Node;

            if (ifStatement.IsSimpleIf() &&
                !IsPartOfLazyInitialization(ifStatement))
            {
                EqualsToNullExpression equalsToNull;
                if (EqualsToNullExpression.TryCreate(ifStatement.Condition, out equalsToNull))
                {
                    SimpleAssignmentExpression assignment;
                    if (SimpleAssignmentExpression.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment) &&
                        assignment.Left.IsEquivalentTo(equalsToNull.Left, topLevel: false) &&
                        assignment.Right.IsSingleLine() &&
                        !ifStatement.SpanContainsDirectives())
                    {
                        StatementSyntax fixableStatement = ifStatement;

                        SyntaxList <StatementSyntax> statements;
                        if (ifStatement.TryGetContainingList(out statements))
                        {
                            int index = statements.IndexOf(ifStatement);

                            if (index > 0)
                            {
                                StatementSyntax previousStatement = statements[index - 1];

                                if (CanRefactor(previousStatement, ifStatement, equalsToNull.Left, ifStatement.Parent))
                                {
                                    fixableStatement = previousStatement;
                                }
                            }
                        }

                        context.ReportDiagnostic(DiagnosticDescriptors.UseCoalesceExpression, fixableStatement);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private static bool CanRefactor(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement, ReturnStatementSyntax returnStatement)
        {
            SimpleIfStatementWithSingleStatement simpleIf;

            if (SimpleIfStatementWithSingleStatement.TryCreate(ifStatement, out simpleIf))
            {
                EqualsToNullExpression equalsToNull;
                if (EqualsToNullExpression.TryCreate(simpleIf.Condition, out equalsToNull))
                {
                    IdentifierNameSyntax identifierName = GetIdentifierName(equalsToNull.Left);

                    if (identifierName != null)
                    {
                        SemanticModel     semanticModel     = context.SemanticModel;
                        CancellationToken cancellationToken = context.CancellationToken;

                        var fieldSymbol = semanticModel.GetSymbol(identifierName, cancellationToken) as IFieldSymbol;

                        if (fieldSymbol != null)
                        {
                            SimpleAssignmentStatement assignment;
                            if (SimpleAssignmentStatement.TryCreate(simpleIf.SingleStatement, out assignment))
                            {
                                string fieldName = identifierName.Identifier.ValueText;

                                return(assignment.Right.IsSingleLine() &&
                                       IsBackingField(GetIdentifierName(assignment.Left), fieldName, fieldSymbol, semanticModel, cancellationToken) &&
                                       IsBackingField(GetIdentifierName(returnStatement.Expression), fieldName, fieldSymbol, semanticModel, cancellationToken));
                            }
                        }
                    }
                }
            }

            return(false);
        }