private static void AnalyzeArrayCreationExpression(SyntaxNodeAnalysisContext context) { var arrayCreation = (ArrayCreationExpressionSyntax)context.Node; if (arrayCreation.ContainsDiagnostics) { return; } ArrayTypeSyntax arrayType = arrayCreation.Type; if (arrayType.ContainsDirectives) { return; } SeparatedSyntaxList <ExpressionSyntax> expressions = arrayCreation.Initializer?.Expressions ?? default; if (!expressions.Any()) { return; } if (AnalyzerOptions.UseImplicitlyTypedArrayWhenTypeIsObvious.IsEnabled(context)) { foreach (ExpressionSyntax expression in expressions) { if (!CSharpTypeAnalysis.IsTypeObvious(expression, null, context.SemanticModel, context.CancellationToken)) { return; } } } TypeSyntax elementType = arrayType.ElementType; SyntaxList <ArrayRankSpecifierSyntax> rankSpecifiers = arrayType.RankSpecifiers; TextSpan textSpan = TextSpan.FromBounds( elementType.SpanStart, ((rankSpecifiers.Count > 1) ? rankSpecifiers.LastButOne() : (SyntaxNode)elementType).Span.End); Location location = Location.Create(arrayCreation.SyntaxTree, textSpan); DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.ReportOnly.UseImplicitlyTypedArray, location); DiagnosticHelpers.ReportObsolete(context, location, AnalyzerOptions.UseImplicitlyTypedArray); DiagnosticHelpers.ReportObsolete(context, location, AnalyzerOptions.UseImplicitlyTypedArrayWhenTypeIsObvious); }
private static void AnalyzeImplicitArrayCreationExpression(SyntaxNodeAnalysisContext context, ArrayCreationTypeStyle kind) { var expression = (ImplicitArrayCreationExpressionSyntax)context.Node; if (expression.ContainsDiagnostics) { return; } if (expression.NewKeyword.ContainsDirectives) { return; } if (expression.OpenBracketToken.ContainsDirectives) { return; } if (expression.CloseBracketToken.ContainsDirectives) { return; } IArrayTypeSymbol arrayTypeSymbol = null; if (kind == ArrayCreationTypeStyle.ImplicitWhenTypeIsObvious) { InitializerExpressionSyntax initializer = expression.Initializer; if (initializer != null) { var isObvious = false; foreach (ExpressionSyntax expression2 in initializer.Expressions) { if (arrayTypeSymbol == null) { arrayTypeSymbol = context.SemanticModel.GetTypeSymbol(expression, context.CancellationToken) as IArrayTypeSymbol; if (arrayTypeSymbol?.ElementType.SupportsExplicitDeclaration() != true) { return; } } isObvious = CSharpTypeAnalysis.IsTypeObvious(expression2, arrayTypeSymbol.ElementType, includeNullability: true, context.SemanticModel, context.CancellationToken); if (!isObvious) { break; } } if (isObvious) { return; } } } if (arrayTypeSymbol == null) { arrayTypeSymbol = context.SemanticModel.GetTypeSymbol(expression, context.CancellationToken) as IArrayTypeSymbol; if (arrayTypeSymbol?.ElementType.SupportsExplicitDeclaration() != true) { return; } } Location location = Location.Create(expression.SyntaxTree, TextSpan.FromBounds(expression.NewKeyword.SpanStart, expression.CloseBracketToken.Span.End)); DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.UseExplicitlyOrImplicitlyTypedArray, location, "explicitly"); }
private static void AnalyzeArrayCreationExpression(SyntaxNodeAnalysisContext context, ArrayCreationTypeStyle kind) { var arrayCreation = (ArrayCreationExpressionSyntax)context.Node; if (arrayCreation.ContainsDiagnostics) { return; } ArrayTypeSyntax arrayType = arrayCreation.Type; if (arrayType.ContainsDirectives) { return; } SeparatedSyntaxList <ExpressionSyntax> expressions = arrayCreation.Initializer?.Expressions ?? default; if (!expressions.Any()) { return; } ITypeSymbol typeSymbol = null; if (kind == ArrayCreationTypeStyle.ImplicitWhenTypeIsObvious) { foreach (ExpressionSyntax expression in expressions) { if (typeSymbol == null) { typeSymbol = context.SemanticModel.GetTypeSymbol(arrayCreation.Type.ElementType, context.CancellationToken); if (typeSymbol?.IsErrorType() != false) { return; } } if (!CSharpTypeAnalysis.IsTypeObvious(expression, typeSymbol, includeNullability: true, context.SemanticModel, context.CancellationToken)) { return; } } } TypeSyntax elementType = arrayType.ElementType; SyntaxList <ArrayRankSpecifierSyntax> rankSpecifiers = arrayType.RankSpecifiers; TextSpan textSpan = TextSpan.FromBounds( elementType.SpanStart, ((rankSpecifiers.Count > 1) ? rankSpecifiers.LastButOne() : (SyntaxNode)elementType).Span.End); Location location = Location.Create(arrayCreation.SyntaxTree, textSpan); DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.UseExplicitlyOrImplicitlyTypedArray, location, "implicitly"); }
private static void AnalyzeImplicitArrayCreationExpression(SyntaxNodeAnalysisContext context) { var expression = (ImplicitArrayCreationExpressionSyntax)context.Node; if (expression.ContainsDiagnostics) { return; } if (expression.NewKeyword.ContainsDirectives) { return; } if (expression.OpenBracketToken.ContainsDirectives) { return; } if (expression.CloseBracketToken.ContainsDirectives) { return; } if (AnalyzerOptions.UseImplicitlyTypedArrayWhenTypeIsObvious.IsEnabled(context)) { InitializerExpressionSyntax initializer = expression.Initializer; if (initializer != null) { var isObvious = false; foreach (ExpressionSyntax expression2 in initializer.Expressions) { isObvious = CSharpTypeAnalysis.IsTypeObvious(expression2, null, context.SemanticModel, context.CancellationToken); if (!isObvious) { break; } } if (isObvious) { return; } } } if (!(context.SemanticModel.GetTypeSymbol(expression, context.CancellationToken) is IArrayTypeSymbol arrayTypeSymbol)) { return; } if (!arrayTypeSymbol.ElementType.SupportsExplicitDeclaration()) { return; } DiagnosticHelpers.ReportDiagnostic( context, DiagnosticDescriptors.UseExplicitlyTypedArrayOrViceVersa, Location.Create(expression.SyntaxTree, TextSpan.FromBounds(expression.NewKeyword.SpanStart, expression.CloseBracketToken.Span.End))); }