示例#1
0
        private void PreventUnnecessaryAllowedListing(
            SymbolAnalysisContext context,
            ImmutableArray <INamedTypeSymbol> disallowedTypes,
            AllowedTypeList allowedTypeList
            )
        {
            if (!(context.Symbol is INamedTypeSymbol namedType))
            {
                return;
            }

            if (!allowedTypeList.Contains(namedType))
            {
                return;
            }

            Location diagnosticLocation = null;

            foreach (var syntaxRef in namedType.DeclaringSyntaxReferences)
            {
                var typeSyntax = syntaxRef.GetSyntax(context.CancellationToken) as TypeDeclarationSyntax;

                diagnosticLocation = diagnosticLocation ?? typeSyntax.Identifier.GetLocation();

                SemanticModel model = context.Compilation.GetSemanticModel(typeSyntax.SyntaxTree);

                bool usesDisallowedTypes = typeSyntax
                                           .DescendantNodes()
                                           .OfType <IdentifierNameSyntax>()
                                           .Any(syntax => IdentifierIsOfDisallowedType(model, disallowedTypes, syntax));

                if (usesDisallowedTypes)
                {
                    return;
                }
            }

            if (diagnosticLocation != null)
            {
                allowedTypeList.ReportEntryAsUnnecesary(
                    entry: namedType,
                    location: diagnosticLocation,
                    report: context.ReportDiagnostic
                    );
            }
        }
示例#2
0
        private void AnalyzeAttribute(
            SyntaxNodeAnalysisContext context,
            INamedTypeSymbol jsonParamBinderT,
            AllowedTypeList allowedList
            )
        {
            if (!(context.Node is AttributeSyntax attribute))
            {
                return;
            }

            if (!AttributeIsOfDisallowedType(context.SemanticModel, jsonParamBinderT, attribute))
            {
                return;
            }

            ISymbol methodSymbol = context.ContainingSymbol;

            if (methodSymbol.Kind != SymbolKind.Method)
            {
                return;
            }

            if (!(methodSymbol.ContainingType is INamedTypeSymbol classSymbol))
            {
                return;
            }

            if (allowedList.Contains(classSymbol))
            {
                return;
            }

            Location   location   = attribute.GetLocation();
            Diagnostic diagnostic = Diagnostic.Create(
                Diagnostics.ObsoleteJsonParamBinder,
                location
                );

            context.ReportDiagnostic(diagnostic);
        }