Skip to content

Commit 062524b

Browse files
committed
removed using static check & added nested enum test
1 parent 3d4acb1 commit 062524b

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

VSDiagnostics/VSDiagnostics/VSDiagnostics.Test/Tests/General/SwitchDoesNotHandleAllEnumOptionsTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,5 +771,60 @@ void Method()
771771
VerifyDiagnostic(original, SwitchDoesNotHandleAllEnumOptionsAnalyzer.Rule.MessageFormat.ToString());
772772
VerifyFix(original, result);
773773
}
774+
775+
[TestMethod]
776+
public void SwitchDoesNotHandleAllEnumOptions_MissingEnumStatement_NestedEnum()
777+
{
778+
var original = @"
779+
namespace ConsoleApplication1
780+
{
781+
class MyClass
782+
{
783+
enum MyEnum
784+
{
785+
Fizz, Buzz, FizzBuzz
786+
}
787+
788+
void Method()
789+
{
790+
var e = MyEnum.Fizz;
791+
switch (e)
792+
{
793+
case MyEnum.Fizz:
794+
case MyEnum.Buzz:
795+
break;
796+
}
797+
}
798+
}
799+
}";
800+
801+
var result = @"
802+
namespace ConsoleApplication1
803+
{
804+
class MyClass
805+
{
806+
enum MyEnum
807+
{
808+
Fizz, Buzz, FizzBuzz
809+
}
810+
811+
void Method()
812+
{
813+
var e = MyEnum.Fizz;
814+
switch (e)
815+
{
816+
case MyEnum.FizzBuzz:
817+
throw new System.NotImplementedException();
818+
case MyEnum.Fizz:
819+
case MyEnum.Buzz:
820+
break;
821+
}
822+
}
823+
}
824+
}";
825+
826+
VerifyDiagnostic(original, SwitchDoesNotHandleAllEnumOptionsAnalyzer.Rule.MessageFormat.ToString());
827+
VerifyFix(original, result);
828+
}
774829
}
775830
}

VSDiagnostics/VSDiagnostics/VSDiagnostics/Diagnostics/General/SwitchDoesNotHandleAllEnumOptions/SwitchDoesNotHandleAllEnumOptionsCodeFix.cs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private async Task<Solution> AddMissingCaseAsync(Document document, CompilationU
4141

4242
var switchBlock = (SwitchStatementSyntax)statement;
4343

44-
var enumType = semanticModel.GetTypeInfo(switchBlock.Expression).Type as INamedTypeSymbol;
44+
var enumType = (INamedTypeSymbol) semanticModel.GetTypeInfo(switchBlock.Expression).Type;
4545
var caseLabels = switchBlock.Sections.SelectMany(l => l.Labels)
4646
.OfType<CaseSwitchLabelSyntax>()
4747
.Select(l => l.Value)
@@ -50,9 +50,8 @@ private async Task<Solution> AddMissingCaseAsync(Document document, CompilationU
5050
var missingLabels = GetMissingLabels(caseLabels, enumType);
5151

5252
// use simplified form if there are any in simplified form or if there are not any labels at all
53-
var useSimplifiedForm = (caseLabels.OfType<IdentifierNameSyntax>().Any() ||
54-
!caseLabels.OfType<MemberAccessExpressionSyntax>().Any()) &&
55-
EnumIsUsingStatic(root, enumType);
53+
var hasSimplifiedLabel = caseLabels.OfType<IdentifierNameSyntax>().Any();
54+
var useSimplifiedForm = hasSimplifiedLabel || !caseLabels.OfType<MemberAccessExpressionSyntax>().Any();
5655

5756
var qualifier = GetQualifierForException(root);
5857

@@ -65,10 +64,10 @@ private async Task<Solution> AddMissingCaseAsync(Document document, CompilationU
6564

6665
foreach (var label in missingLabels)
6766
{
68-
// ReSharper disable once PossibleNullReferenceException
67+
// If an existing simplified label exists, it means we can assume that works already and do it ourselves as well (ergo: there is a static using)
6968
var caseLabel =
7069
SyntaxFactory.CaseSwitchLabel(
71-
SyntaxFactory.ParseExpression(useSimplifiedForm ? $" {label}" : $" {enumType.Name}.{label}")
70+
SyntaxFactory.ParseExpression(hasSimplifiedLabel ? $"{label}" : $"{enumType.Name}.{label}")
7271
.WithTrailingTrivia(SyntaxFactory.ParseTrailingTrivia(Environment.NewLine)));
7372

7473
var section =
@@ -88,28 +87,6 @@ private async Task<Solution> AddMissingCaseAsync(Document document, CompilationU
8887
return newDocument.Project.Solution;
8988
}
9089

91-
private bool EnumIsUsingStatic(CompilationUnitSyntax root, INamedTypeSymbol enumType)
92-
{
93-
var fullyQualifiedName = enumType.Name;
94-
95-
var containingNamespace = enumType.ContainingNamespace;
96-
while (!string.IsNullOrEmpty(containingNamespace.Name))
97-
{
98-
fullyQualifiedName = fullyQualifiedName.Insert(0, containingNamespace.Name + ".");
99-
containingNamespace = containingNamespace.ContainingNamespace;
100-
}
101-
102-
return root.Usings.Any(u =>
103-
{
104-
if (!u.StaticKeyword.IsKind(SyntaxKind.StaticKeyword)) { return false; }
105-
106-
var name = u.Name as QualifiedNameSyntax;
107-
if (name == null) { return false; }
108-
109-
return new string(name.GetText().ToString().ToCharArray().Where(c => !char.IsWhiteSpace(c)).ToArray()) == fullyQualifiedName;
110-
});
111-
}
112-
11390
private IEnumerable<string> GetMissingLabels(List<ExpressionSyntax> caseLabels, INamedTypeSymbol enumType)
11491
{
11592
// these are the labels like `MyEnum.EnumMember`
@@ -130,7 +107,7 @@ private string GetQualifierForException(CompilationUnitSyntax root)
130107
var qualifier = "System.";
131108
var usingSystemDirective =
132109
root.Usings.Where(u => u.Name is IdentifierNameSyntax)
133-
.FirstOrDefault(u => ((IdentifierNameSyntax) u.Name).Identifier.ValueText == "System");
110+
.FirstOrDefault(u => ((IdentifierNameSyntax) u.Name).Identifier.ValueText == nameof(System));
134111

135112
if (usingSystemDirective != null)
136113
{

0 commit comments

Comments
 (0)