Permalink
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (47 sloc) 1.99 KB
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslynator.CSharp;
namespace Roslynator.CSharp.Analysis
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class RemoveUnnecessaryCaseLabelAnalyzer : BaseDiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get { return ImmutableArray.Create(DiagnosticDescriptors.RemoveUnnecessaryCaseLabel); }
}
public override void Initialize(AnalysisContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
base.Initialize(context);
context.RegisterSyntaxNodeAction(AnalyzeSwitchSection, SyntaxKind.SwitchSection);
}
public static void AnalyzeSwitchSection(SyntaxNodeAnalysisContext context)
{
var switchSection = (SwitchSectionSyntax)context.Node;
if (!switchSection.IsParentKind(SyntaxKind.SwitchStatement))
return;
SyntaxList<SwitchLabelSyntax> labels = switchSection.Labels;
if (labels.Count <= 1)
return;
if (!labels.Any(SyntaxKind.DefaultSwitchLabel))
return;
foreach (SwitchLabelSyntax label in labels)
{
if (!label.IsKind(SyntaxKind.DefaultSwitchLabel)
&& label.Keyword.TrailingTrivia.IsEmptyOrWhitespace()
&& label.ColonToken.LeadingTrivia.IsEmptyOrWhitespace())
{
context.ReportDiagnostic(DiagnosticDescriptors.RemoveUnnecessaryCaseLabel, label);
}
}
}
}
}