Ejemplo n.º 1
0
        private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
        {
            var method = (MethodDeclarationSyntax)context.Node;

            if (method.Identifier.Text == "ToDebuggerDisplay")
            {
                // Analyzing ToDebuggerDisplay methods causes InvalidCastException in the middle of Roslyn infrastructure.
                return;
            }

            if (ReadOnlyAnalyzer.MethodCanBeReadOnly(method, context.SemanticModel))
            {
                var methodSymbol = context.SemanticModel.GetDeclaredSymbol(method);
                if (methodSymbol is null)
                {
                    return;
                }
                if (ReadOnlyAnalyzer.StructCanBeReadOnly(methodSymbol.ContainingType, context.SemanticModel))
                {
                    // Do not emit the diagnostic if the entire struct can be made readonly.
                    return;
                }

                var largeStructThreshold = Settings.GetLargeStructThresholdOrDefault(method.TryGetAnalyzerConfigOptions(context.Options));
                ReportAnalyzerForLargeStruct(context, largeStructThreshold, methodSymbol, $"method '{method.Identifier.Text}'", method.Identifier.GetLocation());
            }
        }
Ejemplo n.º 2
0
        private void AnalyzeStructDeclaration(SymbolAnalysisContext context)
        {
            var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;

            if (!context.TryGetSemanticModel(out var semanticModel))
            {
                return;
            }

            if (ReadOnlyAnalyzer.StructCanBeReadOnly(namedTypeSymbol, semanticModel))
            {
                var diagnostic = Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], namedTypeSymbol.Name);

                context.ReportDiagnostic(diagnostic);
            }
        }
        private void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
        {
            var property = (PropertyDeclarationSyntax)context.Node;

            if (!property.IsGetOnlyAutoProperty() && // Excluding int X {get;} because it can be made readonly, but it is already readonly.
                ReadOnlyAnalyzer.PropertyCanBeReadOnly(property, context.SemanticModel))
            {
                var propertySymbol = context.SemanticModel.GetDeclaredSymbol(property);

                if (propertySymbol != null && ReadOnlyAnalyzer.StructCanBeReadOnly(propertySymbol.ContainingType, context.SemanticModel))
                {
                    // Do not emit the diagnostic if the entire struct can be made readonly.
                    return;
                }

                var memberName = $"property '{property.Identifier.Text}'";
                context.ReportDiagnostic(Diagnostic.Create(Rule, property.Identifier.GetLocation(), memberName));
            }
        }
Ejemplo n.º 4
0
        private static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
        {
            var property = (PropertyDeclarationSyntax)context.Node;

            if (!property.IsGetOnlyAutoProperty() && // Excluding int X {get;} because it can be made readonly, but it is already readonly.
                ReadOnlyAnalyzer.PropertyCanBeReadOnly(property, context.SemanticModel))
            {
                var propertySymbol = context.SemanticModel.GetDeclaredSymbol(property);
                if (propertySymbol is null)
                {
                    return;
                }

                if (ReadOnlyAnalyzer.StructCanBeReadOnly(propertySymbol.ContainingType, context.SemanticModel))
                {
                    // Do not emit the diagnostic if the entire struct can be made readonly.
                    return;
                }

                var largeStructThreshold = Settings.GetLargeStructThresholdOrDefault(property.TryGetAnalyzerConfigOptions(context.Options));
                ReportAnalyzerForLargeStruct(context, largeStructThreshold, propertySymbol, $"property '{property.Identifier.Text}'", property.Identifier.GetLocation());
            }
        }
        private void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
        {
            var method = (MethodDeclarationSyntax)context.Node;

            if (method.Identifier.Text == "ToDebuggerDisplay")
            {
                // Analyzing ToDebuggerDisplay methods causes InvalidCastException in the middle of Roslyn infrastructure.
                return;
            }

            if (ReadOnlyAnalyzer.MethodCanBeReadOnly(method, context.SemanticModel))
            {
                var methodSymbol = context.SemanticModel.GetDeclaredSymbol(method);
                if (methodSymbol != null &&
                    ReadOnlyAnalyzer.StructCanBeReadOnly(methodSymbol.ContainingType, context.SemanticModel))
                {
                    // Do not emit the diagnostic if the entire struct can be made readonly.
                    return;
                }

                var memberName = $"method '{method.Identifier.Text}'";
                context.ReportDiagnostic(Diagnostic.Create(Rule, method.Identifier.GetLocation(), memberName));
            }
        }