public static bool IsDefaultSwitchLabel(SwitchLabelSyntax node) { // default: if (node.IsKind(SyntaxKind.DefaultSwitchLabel)) { return(true); } if (node.IsKind(SyntaxKind.CasePatternSwitchLabel, out CasePatternSwitchLabelSyntax? @case)) { // case _: if (@case.Pattern.IsKind(SyntaxKind.DiscardPattern)) { return(@case.WhenClause == null); } // case var _: // case var x: if (@case.Pattern.IsKind(SyntaxKind.VarPattern, out VarPatternSyntax? varPattern) && varPattern.Designation.IsKind(SyntaxKind.DiscardDesignation, SyntaxKind.SingleVariableDesignation)) { return(@case.WhenClause == null); } } return(false); }
public static void Analyze(SyntaxNodeAnalysisContext context, SwitchSectionSyntax switchSection) { SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels; for (int i = 0; i < labels.Count - 1; i++) { SwitchLabelSyntax label = labels[i]; if (label.IsKind(SyntaxKind.DefaultSwitchLabel)) { TextSpan span = TextSpan.FromBounds(label.Span.End, labels.Last().Span.Start); if (!switchSection.ContainsDirectives(span)) { context.ReportDiagnostic( DiagnosticDescriptors.DefaultLabelShouldBeLastLabelInSwitchSection, label); } } } }