@@ -30,16 +30,24 @@ public sealed override FixAllProvider GetFixAllProvider() =>
3030
3131 public sealed override async Task RegisterCodeFixesAsync ( CodeFixContext context )
3232 {
33- SyntaxNode root = await context . Document . GetSyntaxRootAsync ( context . CancellationToken ) . ConfigureAwait ( false ) ;
33+ SyntaxNode ? root = await context . Document . GetSyntaxRootAsync ( context . CancellationToken ) . ConfigureAwait ( false ) ;
34+ if ( root is null )
35+ {
36+ return ;
37+ }
3438
3539 Diagnostic diagnostic = context . Diagnostics . First ( ) ;
3640 TextSpan diagnosticSpan = diagnostic . Location . SourceSpan ;
3741
3842 // The diagnostic span covers the full invocation expression (Directory.GetFiles(...))
39- InvocationExpressionSyntax invocation = root . FindToken ( diagnosticSpan . Start )
40- . Parent . AncestorsAndSelf ( )
43+ InvocationExpressionSyntax ? invocation = root . FindToken ( diagnosticSpan . Start )
44+ . Parent ? . AncestorsAndSelf ( )
4145 . OfType < InvocationExpressionSyntax > ( )
42- . First ( ) ;
46+ . FirstOrDefault ( ) ;
47+ if ( invocation is null )
48+ {
49+ return ;
50+ }
4351
4452 bool isGetFiles = diagnostic . Id == Analyzers . FavorDirectoryEnumerationCalls . DiagnosticId301 ;
4553 string title = isGetFiles ? TitleGetFiles : TitleGetDirectories ;
@@ -61,7 +69,7 @@ private static async Task<Document> UseEnumerationMethodAsync(
6169 {
6270 var memberAccess = ( MemberAccessExpressionSyntax ) invocation . Expression ;
6371
64- SemanticModel semanticModel = await document . GetSemanticModelAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
72+ SemanticModel ? semanticModel = await document . GetSemanticModelAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
6573
6674 // Rename: Directory.GetFiles(...) → Directory.EnumerateFiles(...)
6775 InvocationExpressionSyntax renamedInvocation = invocation . WithExpression (
@@ -76,7 +84,8 @@ private static async Task<Document> UseEnumerationMethodAsync(
7684 SyntaxFactory . IdentifierName ( "ToArray" ) ) )
7785 : renamedInvocation ;
7886
79- SyntaxNode oldRoot = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
87+ SyntaxNode oldRoot = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false )
88+ ?? throw new System . InvalidOperationException ( "Could not get syntax root" ) ;
8089 SyntaxNode newRoot = oldRoot . ReplaceNode ( invocation , replacement . WithAdditionalAnnotations ( Formatter . Annotation ) ) ;
8190
8291 if ( replacement != renamedInvocation && newRoot is CompilationUnitSyntax compilationUnit )
@@ -89,10 +98,14 @@ private static async Task<Document> UseEnumerationMethodAsync(
8998
9099 private static bool NeedsToArrayWrapper (
91100 InvocationExpressionSyntax invocation ,
92- SemanticModel semanticModel ,
101+ SemanticModel ? semanticModel ,
93102 CancellationToken ct )
94103 {
95- SyntaxNode parent = invocation . Parent ;
104+ if ( semanticModel is null )
105+ {
106+ return false ;
107+ }
108+ SyntaxNode ? parent = invocation . Parent ;
96109
97110 // string[] files = Directory.GetFiles(...) or field/property initializer
98111 if ( parent is EqualsValueClauseSyntax equalsValue )
@@ -123,7 +136,7 @@ private static bool NeedsToArrayWrapper(
123136 // return Directory.GetFiles(...) in a method or local function returning string[]
124137 if ( parent is ReturnStatementSyntax )
125138 {
126- TypeSyntax returnType = invocation . Ancestors ( )
139+ TypeSyntax ? returnType = invocation . Ancestors ( )
127140 . Select ( a => a switch
128141 {
129142 MethodDeclarationSyntax m => m . ReturnType ,
@@ -141,7 +154,7 @@ private static bool NeedsToArrayWrapper(
141154 // Expression-bodied members: string[] GetFiles() => Directory.GetFiles(...)
142155 if ( parent is ArrowExpressionClauseSyntax arrow )
143156 {
144- TypeSyntax returnType = arrow . Parent switch
157+ TypeSyntax ? returnType = arrow . Parent switch
145158 {
146159 MethodDeclarationSyntax m => m . ReturnType ,
147160 LocalFunctionStatementSyntax lf => lf . ReturnType ,
@@ -160,7 +173,7 @@ private static bool NeedsToArrayWrapper(
160173 && argumentList . Parent is InvocationExpressionSyntax outerInvocation
161174 && semanticModel . GetSymbolInfo ( outerInvocation , ct ) . Symbol is IMethodSymbol outerMethod )
162175 {
163- IParameterSymbol targetParam ;
176+ IParameterSymbol ? targetParam ;
164177
165178 // Named argument: SomeMethod(param: Directory.GetFiles(...))
166179 if ( argument . NameColon != null )
0 commit comments