@@ -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