Пример #1
0
        public void Analyze(SemanticModel model)
        {
            var constructorFlowAnalyzer = new CtorFlowAnalyzer(model);
            var root    = model.SyntaxTree.GetRoot();
            var classes = root.DescendantNodes().OfType <ClassDeclarationSyntax>();

            foreach (var @class in classes)
            {
                var classSymbol = model.GetDeclaredSymbol(@class);
                if (classSymbol != null && classSymbol.HasNotNull())
                {
                    context.ReportDiagnostic(MainAnalyzer.CreateBadAttributeUsageError(@class.GetLocation(), false));
                }
                var members = GetTrackedMembers(model, @class);
                if (members.Item1.Count > 0)
                {
                    FlagUninitializedFields(@class, constructorFlowAnalyzer, members.Item1);
                }
                if (members.Item2.Count > 0)
                {
                    VerifyMethods(members.Item2);
                }
                if (members.Item3.Count > 0)
                {
                    VerifyExpressionBodies(members.Item3);
                }
            }
        }
Пример #2
0
        private void FlagUninitializedFields(
            ClassDeclarationSyntax @class,
            CtorFlowAnalyzer constructorFlowAnalyzer,
            Dictionary <ISymbol, NotNullFieldInfo> fields)
        {
            var flowAnalysis = constructorFlowAnalyzer.AnalyzeDataFlow(@class, fields);

            foreach (var member in fields.Values)
            {
                var isInitialized = member.Initializer != null &&
                                    member.Initializer.Value.GetTypeOfValue(context.SemanticModel) == ValueType.NotNull;
                if (!isInitialized)
                {
                    if (flowAnalysis.Count == 0)
                    {
                        context.ReportDiagnostic(MainAnalyzer.CreateMemberNotInitialized(member.Location, member.Symbol));
                        continue;
                    }
                    foreach (var flow in flowAnalysis)
                    {
                        if (flow.UnassignedMembers.Contains(member))
                        {
                            context.ReportDiagnostic(MainAnalyzer.CreateMemberNotInitialized(flow.Constructor.GetLocation(), member.Symbol));
                        }
                    }
                }
            }
        }