public static void Analyze(SyntaxNodeAnalysisContext context, InitializerExpressionSyntax initializer)
 {
     if (initializer.Expressions.Count == 2)
     {
         context.ReportDiagnostic(
             DiagnosticDescriptors.UseCSharp6DictionaryInitializer,
             initializer.GetLocation());
     }
 }
        public static void Analyze(SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax objectCreationExpression)
        {
            TypeSyntax type = objectCreationExpression.Type;
            InitializerExpressionSyntax initializer = objectCreationExpression.Initializer;

            if (type?.IsMissing == false &&
                initializer?.Expressions.Any() == false &&
                initializer.OpenBraceToken.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()) &&
                initializer.CloseBraceToken.LeadingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.RemoveEmptyInitializer,
                    initializer.GetLocation());
            }
        }
Exemple #3
0
        private void AnalyzeObjectCreationExpression(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var objectCreationExpression = (ObjectCreationExpressionSyntax)context.Node;

            if (objectCreationExpression.Type == null || objectCreationExpression.Initializer == null)
            {
                return;
            }

            InitializerExpressionSyntax initializer = objectCreationExpression.Initializer;

            if (initializer.Expressions.Count == 0 &&
                initializer.OpenBraceToken.TrailingTrivia.IsWhitespaceOrEndOfLine() &&
                initializer.CloseBraceToken.LeadingTrivia.IsWhitespaceOrEndOfLine())
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.RemoveEmptyObjectInitializer,
                    initializer.GetLocation());
            }

            ArgumentListSyntax argumentList = objectCreationExpression.ArgumentList;

            if (argumentList == null)
            {
                Location location = Location.Create(
                    context.Node.SyntaxTree,
                    new TextSpan(objectCreationExpression.Type.Span.End, 1));

                context.ReportDiagnostic(
                    DiagnosticDescriptors.AddConstructorArgumentList,
                    location);
            }
            else if (argumentList.Arguments.Count == 0 &&
                     !argumentList.OpenParenToken.IsMissing &&
                     !argumentList.CloseParenToken.IsMissing &&
                     argumentList.OpenParenToken.TrailingTrivia.IsWhitespaceOrEndOfLine() &&
                     argumentList.CloseParenToken.LeadingTrivia.IsWhitespaceOrEndOfLine())
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.RemoveEmptyArgumentList,
                    argumentList.GetLocation());
            }
        }
 public override void VisitInitializerExpression(InitializerExpressionSyntax node)
 {
     if (entryNode is AnonymousFunctionExpressionSyntax && embeddedNode is AnonymousFunctionExpressionSyntax)
     {
         return;
     }
     if (_weComeFromMethod && _weInAnonymousMethod)
     {
         return;
     }
     if (node.IsParent <AnonymousObjectCreationExpressionSyntax>())
     {
         return;
     }
     InsertLLOCMap(node.GetLocation());
     base.VisitInitializerExpression(node);
 }
 public override void VisitInitializerExpression(InitializerExpressionSyntax node)
 {
     InsertLLOCMap(node.GetLocation());
     base.VisitInitializerExpression(node);
 }