private static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
        {
            var switchStatement = (SwitchStatementSyntax)context.Node;

            if (switchStatement.ContainsDiagnostics)
            {
                return;
            }

            SwitchSectionSyntax defaultSection = switchStatement.DefaultSection();

            if (defaultSection == null)
            {
                return;
            }

            if (!ContainsOnlyBreakStatement(defaultSection))
            {
                return;
            }

            if (switchStatement.DescendantNodes(switchStatement.Sections.Span).Any(f => f.IsKind(SyntaxKind.GotoDefaultStatement)))
            {
                return;
            }

            if (!defaultSection
                .DescendantTrivia(defaultSection.Span)
                .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.RemoveRedundantDefaultSwitchSection, defaultSection);
        }
 private static void AnalyzeRedundantDefaultSwitchSection(SyntaxNodeAnalysisContext context, SwitchSectionSyntax switchSection)
 {
     if (switchSection.Labels.Any(SyntaxKind.DefaultSwitchLabel) &&
         ContainsOnlyBreakStatement(switchSection) &&
         switchSection
         .DescendantTrivia(switchSection.Span)
         .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
     {
         context.ReportDiagnostic(
             DiagnosticDescriptors.RemoveRedundantDefaultSwitchSection,
             switchSection.GetLocation());
     }
 }
Example #3
0
        internal static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
        {
            var switchStatement = (SwitchStatementSyntax)context.Node;

            if (!switchStatement.ContainsDiagnostics)
            {
                SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

                SwitchSectionSyntax defaultSection = FindDefaultSection(sections);

                if (defaultSection != null &&
                    ContainsOnlyBreakStatement(defaultSection) &&
                    !switchStatement.DescendantNodes(sections.Span).Any(f => f.IsKind(SyntaxKind.GotoDefaultStatement)) &&
                    defaultSection
                    .DescendantTrivia(defaultSection.Span)
                    .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                {
                    context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantDefaultSwitchSection, defaultSection);
                }
            }
        }