Beispiel #1
0
        private static void AnalyzeTypeDeclaration(
            SymbolAnalysisContext ctx,
            AnnotationsContext annotationsContext,
            ImmutabilityContext immutabilityContext,
            INamedTypeSymbol typeSymbol
            )
        {
            if (typeSymbol.IsImplicitlyDeclared)
            {
                return;
            }

            ImmutableAttributeConsistencyChecker consistencyChecker = new ImmutableAttributeConsistencyChecker(
                compilation: ctx.Compilation,
                diagnosticSink: ctx.ReportDiagnostic,
                context: immutabilityContext,
                annotationsContext: annotationsContext
                );

            consistencyChecker.CheckTypeDeclaration(typeSymbol);

            if (typeSymbol.TypeKind == TypeKind.Interface)
            {
                return;
            }

            if (!annotationsContext.Objects.Immutable.IsDefined(typeSymbol) &&
                !annotationsContext.Objects.ConditionallyImmutable.IsDefined(typeSymbol) &&
                !annotationsContext.Objects.ImmutableBaseClass.IsDefined(typeSymbol)
                )
            {
                return;
            }

            if (annotationsContext.Objects.ConditionallyImmutable.IsDefined(typeSymbol))
            {
                immutabilityContext = immutabilityContext.WithConditionalTypeParametersAsImmutable(typeSymbol);
            }

            ImmutableDefinitionChecker checker = new ImmutableDefinitionChecker(
                compilation: ctx.Compilation,
                diagnosticSink: ctx.ReportDiagnostic,
                context: immutabilityContext,
                annotationsContext: annotationsContext
                );

            checker.CheckDeclaration(typeSymbol);
        }
Beispiel #2
0
        private static void AnalyzeMember(
            SymbolAnalysisContext ctx,
            AnnotationsContext annotationsContext,
            ImmutabilityContext immutabilityContext
            )
        {
            // We only care about checking static fields/properties. These
            // are global variables, so we always want them to be immutable.
            // The fields/properties of [Immutable] types get handled via
            // AnalyzeTypeDeclaration.
            if (!ctx.Symbol.IsStatic)
            {
                return;
            }

            // Ignore const things, which include enum names.
            if (ctx.Symbol is IFieldSymbol f && f.IsConst)
            {
                return;
            }

            // We would like this check to run for generated code too, but
            // there are two problems:
            // (1) the easy one: we generate some static variables that are
            //     safe in practice but don't analyze well.
            // (2) the hard one: resx code-gen generates some stuff that's
            //     safe in practice but doesn't analyze well.
            if (ctx.Symbol.IsFromGeneratedCode())
            {
                return;
            }

            var checker = new ImmutableDefinitionChecker(
                compilation: ctx.Compilation,
                diagnosticSink: ctx.ReportDiagnostic,
                context: immutabilityContext,
                annotationsContext: annotationsContext
                );

            checker.CheckMember(ctx.Symbol);
        }