Exemplo n.º 1
0
        public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            int    startLine  = node.GetLocation().GetLineSpan().StartLinePosition.Line + 1;
            int    endLine    = node.GetLocation().GetLineSpan().EndLinePosition.Line + 1;
            string methodName = node.Identifier.ToString();

            _locationStore.AddLocationRelativeToMethod(startLine - 1, LocationRelativeToMethod.MethodDescription, methodName);
            _locationStore.AddLocationRelativeToMethod(startLine, LocationRelativeToMethod.MethodStart, methodName);
            _locationStore.AddLocationRelativeToMethod(startLine + 1, endLine - 1, LocationRelativeToMethod.MethodInner, methodName);
            _locationStore.AddLocationRelativeToMethod(endLine, LocationRelativeToMethod.MethodEnd, methodName);
            base.VisitConstructorDeclaration(node);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reports a warning if any field isn't initialized with
        /// the initializer, constructor or public setters.
        /// </summary>
        void ReportUninitializedFields(ConstructorDeclarationSyntax constructorDecl)
        {
            var uninitializedSymbols =
                MemberMap.Variables
                .Where(kv => !IsInitialized(kv.Key) && !kv.Value.CanBeUninitialized)
                .Select(kv => kv.Key)
                .ToImmutableArray();

            if (uninitializedSymbols.Length != 0)
            {
                Reporter.ReportConstructorDiagnostic(constructorDecl.GetLocation(), uninitializedSymbols);
            }
        }
        public virtual ISyntaxNodeDeclaration Convert(ConstructorDeclarationSyntax constructorDeclarationSyntax)
        {
            var location         = constructorDeclarationSyntax.GetLocation();
            var locationLineSpan = location.GetLineSpan();
            var declaration      = CreateLocation(locationLineSpan.StartLinePosition);

            var body         = GetBody(constructorDeclarationSyntax);
            var bodyLocation = body.GetLocation();
            var bodyLineSpan = bodyLocation.GetLineSpan();
            var bodyStart    = CreateLocation(bodyLineSpan.StartLinePosition);
            var bodyEnd      = CreateLocation(bodyLineSpan.EndLinePosition);

            return(CreateConstructor(declaration, bodyStart, bodyEnd, body));
        }
Exemplo n.º 4
0
        private void Analyze(SyntaxNodeAnalysisContext context)
        {
            ConstructorDeclarationSyntax ctor = (ConstructorDeclarationSyntax)context.Node;

            if (!ctor.Modifiers.Any(SyntaxKind.StaticKeyword))
            {
                //not a static constructor
                return;
            }

            if (ctor.Body == null)
            {
                //during the intellisense phase the body of a constructor can be non-existent.
                return;
            }

            if (ctor.Body.Statements.Any())
            {
                //not empty
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Rule, ctor.GetLocation()));
        }
Exemplo n.º 5
0
 public EntryPoint(ConstructorDeclarationSyntax constructor)
 {
     Name   = constructor.ToDisplayName();
     LineNo = constructor.GetLocation().GetLineSpan().StartLinePosition.Line;
 }
Exemplo n.º 6
0
 internal static Diagnostic MissingConstructorArgument(ConstructorDeclarationSyntax constructorSyntax) =>
 Create($"{ErrorId}050", constructorSyntax.GetLocation(), "There are no argument given that corresponds to the required formal parameter.");
Exemplo n.º 7
0
        public override TypeWithNode VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            var outerMethodReturnType = currentMethodReturnType;

            try {
                currentMethodReturnType = typeSystem.VoidType;
                var operation = semanticModel.GetOperation(node, cancellationToken);
                if (operation == null)
                {
                    throw new NotSupportedException($"Could not get operation for {node}");
                }
                if (node.Initializer?.ThisOrBaseKeyword.Kind() != SyntaxKind.ThisKeyword)
                {
                    HashSet <ISymbol> initializedSymbols = new HashSet <ISymbol>();
                    foreach (var assgn in operation.DescendantsAndSelf().OfType <ISimpleAssignmentOperation>())
                    {
                        if (assgn.Target is IFieldReferenceOperation fieldRef)
                        {
                            initializedSymbols.Add(fieldRef.Field);
                        }
                        else if (assgn.Target is IPropertyReferenceOperation propertyRef)
                        {
                            initializedSymbols.Add(propertyRef.Property);
                        }
                        else if (assgn.Target is IEventReferenceOperation eventRef)
                        {
                            initializedSymbols.Add(eventRef.Event);
                        }
                    }
                    if (node.Parent is TypeDeclarationSyntax typeSyntax)
                    {
                        bool isStatic = node.Modifiers.Any(SyntaxKind.StaticKeyword);
                        MarkFieldsAndPropertiesAsNullable(typeSyntax.Members, isStatic, initializedSymbols, node.GetLocation());
                    }
                }
                return(operation.Accept(operationVisitor, new EdgeBuildingContext()));
            } finally {
                currentMethodReturnType = outerMethodReturnType;
            }
        }
Exemplo n.º 8
0
        public static void Analyze(SyntaxNodeAnalysisContext context, ConstructorDeclarationSyntax constructor)
        {
            if (constructor.ParameterList?.Parameters.Any() == false &&
                constructor.Body?.Statements.Any() == false)
            {
                SyntaxTokenList modifiers = constructor.Modifiers;

                if (modifiers.Contains(SyntaxKind.PublicKeyword) &&
                    !modifiers.Contains(SyntaxKind.StaticKeyword))
                {
                    ConstructorInitializerSyntax initializer = constructor.Initializer;

                    if (initializer == null ||
                        initializer.ArgumentList?.Arguments.Any() == false)
                    {
                        if (IsSingleInstanceConstructor(constructor) &&
                            constructor
                            .DescendantTrivia(constructor.Span)
                            .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                        {
                            context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantConstructor, constructor.GetLocation());
                        }
                    }
                }
            }
        }