private static bool CheckContainingNode(SyntaxNode node) { switch (node.Kind()) { case SyntaxKind.ForEachStatement: case SyntaxKind.ForStatement: case SyntaxKind.UsingStatement: case SyntaxKind.WhileStatement: case SyntaxKind.DoStatement: case SyntaxKind.LockStatement: case SyntaxKind.FixedStatement: case SyntaxKind.UnsafeStatement: case SyntaxKind.TryStatement: case SyntaxKind.CheckedStatement: case SyntaxKind.UncheckedStatement: return(true); case SyntaxKind.IfStatement: return(IfElseChain.IsTopmostIf((IfStatementSyntax)node)); case SyntaxKind.ElseClause: return(IfElseChain.IsEndOfChain((ElseClauseSyntax)node)); } return(false); }
public static void Analyze(SyntaxNodeAnalysisContext context, ContinueStatementSyntax continueStatement) { SyntaxNode parent = continueStatement.Parent; if (parent?.IsKind(SyntaxKind.Block) == true) { var block = (BlockSyntax)parent; parent = parent.Parent; if (parent != null) { SyntaxKind kind = parent.Kind(); if (CanContainContinueStatement(kind)) { if (block.Statements.IsLast(continueStatement) && !continueStatement.SpanContainsDirectives()) { context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantContinueStatement, continueStatement); } } else if (kind == SyntaxKind.ElseClause) { var elseClause = (ElseClauseSyntax)parent; if (IfElseChain.IsEndOfChain(elseClause)) { IfStatementSyntax ifStatement = IfElseChain.GetTopmostIf(elseClause); parent = ifStatement.Parent; if (parent?.IsKind(SyntaxKind.Block) == true) { block = (BlockSyntax)parent; parent = parent.Parent; if (CanContainContinueStatement(parent) && block.Statements.IsLast(ifStatement) && !elseClause.SpanContainsDirectives()) { context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantContinueStatement, continueStatement); } } } } } } }