private static SwitchStatementSyntax CreateNewSwitchStatement(SwitchStatementSyntax switchStatement) { SyntaxList <SwitchSectionSyntax> newSections = switchStatement .Sections .Select(CreateNewSection) .ToSyntaxList(); return(switchStatement.WithSections(newSections)); SwitchSectionSyntax CreateNewSection(SwitchSectionSyntax section) { SyntaxList <StatementSyntax> statements = section.GetStatements(); if (statements.Last().IsKind(SyntaxKind.ThrowStatement)) { return(section); } var expressionStatement = (ExpressionStatementSyntax)statements.LastButOne(); var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression; section = section.ReplaceNode(expressionStatement, ReturnStatement(assignment.Right).WithTriviaFrom(expressionStatement)); return(section.RemoveStatement(section.GetStatements().Last())); } }
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 AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context) { var switchStatement = (SwitchStatementSyntax)context.Node; StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(switchStatement); if (!statementsInfo.Success) { return; } ReturnStatementSyntax returnStatement = FindReturnStatementBelow(statementsInfo.Statements, switchStatement); ExpressionSyntax expression = returnStatement?.Expression; if (expression == null) { return; } if (switchStatement.SpanOrTrailingTriviaContainsDirectives()) { return; } if (returnStatement.SpanOrLeadingTriviaContainsDirectives()) { return; } SemanticModel semanticModel = context.SemanticModel; CancellationToken cancellationToken = context.CancellationToken; ISymbol symbol = semanticModel.GetSymbol(expression, cancellationToken); if (symbol == null) { return; } if (!IsLocalDeclaredInScopeOrNonRefOrOutParameterOfEnclosingSymbol(symbol, statementsInfo.Parent, semanticModel, cancellationToken)) { return; } foreach (SwitchSectionSyntax section in switchStatement.Sections) { SyntaxList <StatementSyntax> statements = section.GetStatements(); if (!statements.Any()) { return; } switch (statements.Last().Kind()) { case SyntaxKind.ThrowStatement: { continue; } case SyntaxKind.BreakStatement: { if (statements.Count == 1 || !IsSymbolAssignedInStatement(symbol, statements.LastButOne(), semanticModel, cancellationToken)) { return; } break; } default: { return; } } } DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UnnecessaryAssignment, switchStatement); }
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"); }
internal static TNode LastButOneOrDefault <TNode>(this SyntaxList <TNode> list) where TNode : SyntaxNode { return((list.Count > 1) ? list.LastButOne() : default(TNode)); }