Ejemplo n.º 1
0
        private static Diagnostic GetDiagnostic(ElementAccessExpressionSyntax elementAccess)
        {
            if (!elementAccess.IsBeingAssigned())
            {
                return(Diagnostic.Create(RuleForGet, elementAccess.GetLocation(), elementAccess));
            }

            var assignmentExpression = elementAccess.FirstAncestorOrSelf <AssignmentExpressionSyntax>();

            return(Diagnostic.Create(RuleForSet, assignmentExpression.GetLocation(), elementAccess));
        }
Ejemplo n.º 2
0
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            ElementAccessExpressionSyntax elementAccessExpressionSyntax = context.Node as ElementAccessExpressionSyntax;
            Location contextLocation = elementAccessExpressionSyntax.GetLocation();

            if (elementAccessExpressionSyntax != null)
            {
                ISymbol expressionSymbol = context.SemanticModel.GetSymbolInfo(elementAccessExpressionSyntax.Expression).Symbol;
                if (expressionSymbol.Kind == SymbolKind.Parameter)
                {
                    IParameterSymbol parameter = expressionSymbol as IParameterSymbol;
                    if (CheckParameterSymbol(parameter))
                    {
                        var diagnostic = Diagnostic.Create(DiagnosticDescriptors.DoNotModifyReadonlyArray, elementAccessExpressionSyntax.GetLocation());
                        context.ReportDiagnostic(diagnostic);
                    }
                }
                else if (expressionSymbol.Kind == SymbolKind.Method)
                {
                    IMethodSymbol methodSymbol = expressionSymbol as IMethodSymbol;
                    if (CheckMethod(methodSymbol))
                    {
                        var diagnostic = Diagnostic.Create(DiagnosticDescriptors.DoNotModifyReadonlyArray, elementAccessExpressionSyntax.GetLocation());
                        context.ReportDiagnostic(diagnostic);
                    }
                }
                else if (expressionSymbol.Kind == SymbolKind.Local)
                {
                    ILocalSymbol localSymbol = expressionSymbol as ILocalSymbol;
                    if (CheckLocalSymbol(localSymbol))
                    {
                        var diagnostic = Diagnostic.Create(DiagnosticDescriptors.DoNotModifyReadonlyArray, elementAccessExpressionSyntax.GetLocation());
                        context.ReportDiagnostic(diagnostic);
                    }
                }
                else if (expressionSymbol.Kind == SymbolKind.Field)
                {
                    IFieldSymbol fieldSymbol = expressionSymbol as IFieldSymbol;
                    if (CheckFieldSymbol(fieldSymbol))
                    {
                        var diagnostic = Diagnostic.Create(DiagnosticDescriptors.DoNotModifyReadonlyArray, elementAccessExpressionSyntax.GetLocation());
                        context.ReportDiagnostic(diagnostic);
                    }
                }
            }
        }