private void AnalyzeObjectInitSyntax(SyntaxNodeAnalysisContext context)
        {
            var initializer = (InitializerExpressionSyntax)context.Node;

            if (initializer.Parent is AssignmentExpressionSyntax assignment && assignment.Left != null)
            {
                var annotatedParent = SyntaxHelper.FindNearestContainer <ObjectCreationExpressionSyntax, MethodDeclarationSyntax>(initializer.Parent, node => IsMarkedWithComment(node, "FullInitRequired:recursive"));
                if (annotatedParent is null)
                {
                    return;
                }

                var typeInfo = context.SemanticModel.GetTypeInfo(assignment.Left);
                if (typeInfo.Type != null)
                {
                    var membersExtractor         = new MembersExtractor(context.SemanticModel, initializer);
                    var membersForInitialization = membersExtractor.GetAllMembersThatCanBeInitialized(typeInfo.Type).Select(x => x.Name).ToImmutableHashSet();
                    if (membersForInitialization.IsEmpty)
                    {
                        return;
                    }

                    TryToReportMissingMembers(context, initializer, membersForInitialization, initializer.GetLocation());
                }
            }
        }
Ejemplo n.º 2
0
 private static IReadOnlyList <MemberSymbolInfo> GetMembers(MembersExtractor membersExtractor, ITypeSymbol namedType, string namePrefix = null)
 {
     return(membersExtractor.GetAllAccessibleMembers(namedType, x => x switch
     {
         IPropertySymbol property => property.IsStatic == false && property.IsIndexer == false,
         IFieldSymbol field => field.IsImplicitlyDeclared == false && field.IsStatic == false,
         _ => false
     })
Ejemplo n.º 3
0
        public IReadOnlyList <MemberSymbolInfo> GetMissingMembersFor(INamedTypeSymbol namedType)
        {
            var memberExtractor = new MembersExtractor(namedType);

            var ownMembers  = GetMembers(memberExtractor, namedType);
            var twinMembers = GetMembers(memberExtractor, this.Type, NamePrefix).Where(x => IgnoredMembers.Contains(x.Symbol.Name) == false).ToList();

            return(twinMembers.Except(ownMembers).ToList());
        }
Ejemplo n.º 4
0
 private static ImmutableHashSet <string> GetMembers(ITypeSymbol namedType)
 {
     return(MembersExtractor.GetAllMembers(namedType)
            .Where(x => x switch
     {
         IPropertySymbol property => property.IsStatic == false,
         IFieldSymbol field => field.IsImplicitlyDeclared == false && field.IsStatic == false,
         _ => false
     })
        private IEnumerable <ISymbol> GetMembersForRequiredInit(ITypeSymbol type, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
        {
            var membersExtractor = new MembersExtractor(semanticModel, objectCreation);

            if (IsInsideInitBlockWithFullInit(objectCreation) ||
                SymbolHelper.IsMarkedWithAttribute(type, SmartAnnotations.InitRequired) ||
                SymbolHelper.IsMarkedWithAttribute(type, SmartAnnotations.InitOnly))
            {
                return(membersExtractor.GetAllMembersThatCanBeInitialized(type));
            }

            var symbolCache = new SymbolHelperCache();

            return(membersExtractor.GetAllMembersThatCanBeInitialized(type).Where(memberSymbol =>
                                                                                  SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitRequired) ||
                                                                                  SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnly) ||
                                                                                  NonNullableShouldBeInitialized(memberSymbol, symbolCache)));
        }