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