public override void Initialize(AnalysisContext analysisContext)
        {
            analysisContext.EnableConcurrentExecution();
            analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

            analysisContext.RegisterCompilationStartAction(
                (context) =>
            {
                INamedTypeSymbol iCollectionType        = WellKnownTypes.ICollection(context.Compilation);
                INamedTypeSymbol genericICollectionType = WellKnownTypes.GenericICollection(context.Compilation);
                INamedTypeSymbol iEnumerableType        = WellKnownTypes.IEnumerable(context.Compilation);
                INamedTypeSymbol genericIEnumerableType = WellKnownTypes.GenericIEnumerable(context.Compilation);
                INamedTypeSymbol iListType        = WellKnownTypes.IList(context.Compilation);
                INamedTypeSymbol genericIListType = WellKnownTypes.GenericIList(context.Compilation);

                if (iCollectionType == null && genericICollectionType == null &&
                    iEnumerableType == null && genericIEnumerableType == null &&
                    iListType == null && genericIListType == null)
                {
                    return;
                }

                context.RegisterSymbolAction(c => AnalyzeSymbol(c,
                                                                iCollectionType, genericICollectionType,
                                                                iEnumerableType, genericIEnumerableType,
                                                                iListType, genericIListType),
                                             SymbolKind.NamedType);
            });
        }
Example #2
0
        public override void Initialize(AnalysisContext analysisContext)
        {
            analysisContext.EnableConcurrentExecution();

            analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

            analysisContext.RegisterCompilationStartAction(
                (context) =>
            {
                INamedTypeSymbol specializedCollectionsSymbol = context.Compilation.GetTypeByMetadataName(SpecializedCollectionsMetadataName);
                if (specializedCollectionsSymbol == null)
                {
                    // TODO: In the future, we may want to run this analyzer even if the SpecializedCollections
                    // type cannot be found in this compilation. In some cases, we may want to add a reference
                    // to SpecializedCollections as a linked file or an assembly that contains it. With this
                    // check, we will not warn where SpecializedCollections is not yet referenced.
                    return;
                }

                INamedTypeSymbol genericEnumerableSymbol = WellKnownTypes.GenericIEnumerable(context.Compilation);
                if (genericEnumerableSymbol == null)
                {
                    return;
                }

                INamedTypeSymbol linqEnumerableSymbol = context.Compilation.GetTypeByMetadataName(LinqEnumerableMetadataName);
                if (linqEnumerableSymbol == null)
                {
                    return;
                }

                if (!(linqEnumerableSymbol.GetMembers(EmptyMethodName).FirstOrDefault() is IMethodSymbol genericEmptyEnumerableSymbol) ||
                    genericEmptyEnumerableSymbol.Arity != 1 ||
                    genericEmptyEnumerableSymbol.Parameters.Length != 0)
                {
                    return;
                }

                GetCodeBlockStartedAnalyzer(context, genericEnumerableSymbol, genericEmptyEnumerableSymbol);
            });
        }