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));
            }
        }
Esempio n. 2
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());
            }
        }