@@ -60,17 +60,10 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
6060 // 1. Lambda / anonymous function case (PT0002 / PT0003 / PT0004)
6161 // ------------------------------------------------------------------
6262
63- CSharpSyntaxNode ? lambdaNode = node . FirstAncestorOrSelf < ParenthesizedLambdaExpressionSyntax > ( ) ;
63+ var lambdaNode = node . FirstAncestorOrSelf < ParenthesizedLambdaExpressionSyntax > ( ) ??
64+ ( CSharpSyntaxNode ? ) node . FirstAncestorOrSelf < SimpleLambdaExpressionSyntax > ( ) ;
6465
65- if ( lambdaNode is null )
66- {
67- lambdaNode = node . FirstAncestorOrSelf < SimpleLambdaExpressionSyntax > ( ) ;
68- }
69-
70- if ( lambdaNode is null )
71- {
72- lambdaNode = node . FirstAncestorOrSelf < AnonymousMethodExpressionSyntax > ( ) ;
73- }
66+ lambdaNode ??= node . FirstAncestorOrSelf < AnonymousMethodExpressionSyntax > ( ) ;
7467
7568 if ( lambdaNode is not null &&
7669 diagnosticId is AsyncMethodConventionsAnalyzer . CancellationTokenMissingId
@@ -102,7 +95,7 @@ or AsyncMethodConventionsAnalyzer.CancellationTokenNameId
10295 return ;
10396 }
10497
105- var methodSymbol = semanticModel . GetDeclaredSymbol ( methodDecl , cancellationToken ) as IMethodSymbol ;
98+ var methodSymbol = semanticModel . GetDeclaredSymbol ( methodDecl , cancellationToken ) ;
10699 if ( methodSymbol is null )
107100 {
108101 return ;
@@ -152,7 +145,7 @@ private static void RegisterAsyncSuffixFix(CodeFixContext context,
152145 CodeAction . Create (
153146 title ,
154147 c => RenameMethodAsync ( context . Document . Project . Solution , methodSymbol , newName , c ) ,
155- equivalenceKey : "RenameToAsync" ) ,
148+ "RenameToAsync" ) ,
156149 diagnostic ) ;
157150 }
158151
@@ -174,7 +167,7 @@ private static void RegisterCancellationTokenFixesForMethod(CodeFixContext conte
174167 CodeAction . Create (
175168 title ,
176169 c => AddCancellationTokenAsync ( context . Document , methodDecl , c ) ,
177- equivalenceKey : "AddCtParameter" ) ,
170+ "AddCtParameter" ) ,
178171 diagnostic ) ;
179172
180173 return ;
@@ -188,35 +181,39 @@ private static void RegisterCancellationTokenFixesForMethod(CodeFixContext conte
188181 return ;
189182 }
190183
191- if ( diagnosticId == AsyncMethodConventionsAnalyzer . CancellationTokenNameId )
184+ switch ( diagnosticId )
192185 {
193- if ( ! string . Equals ( ctParam . Name , "ct" , StringComparison . Ordinal ) )
186+ case AsyncMethodConventionsAnalyzer . CancellationTokenNameId
187+ when string . Equals ( ctParam . Name , "ct" , StringComparison . Ordinal ) :
188+ return ;
189+ case AsyncMethodConventionsAnalyzer . CancellationTokenNameId :
194190 {
195191 var renameTitle = $ "Rename '{ ctParam . Name } ' to 'ct'";
196192
197193 context . RegisterCodeFix (
198194 CodeAction . Create (
199195 renameTitle ,
200196 c => RenameParameterAsync ( context . Document . Project . Solution , ctParam , "ct" , c ) ,
201- equivalenceKey : "RenameCtParameter" ) ,
197+ "RenameCtParameter" ) ,
202198 diagnostic ) ;
203- }
204199
205- return ;
206- }
207-
208- if ( diagnosticId == AsyncMethodConventionsAnalyzer . CancellationTokenPositionId )
209- {
210- if ( ctParam . Ordinal != methodSymbol . Parameters . Length - 1 )
200+ return ;
201+ }
202+ case AsyncMethodConventionsAnalyzer . CancellationTokenPositionId :
211203 {
212- var moveTitle = "Move CancellationToken parameter to last position" ;
204+ if ( ctParam . Ordinal != methodSymbol . Parameters . Length - 1 )
205+ {
206+ const string moveTitle = "Move CancellationToken parameter to last position" ;
207+
208+ context . RegisterCodeFix (
209+ CodeAction . Create (
210+ moveTitle ,
211+ c => MoveCancellationTokenToLastAsync ( context . Document , methodDecl , ctParam , c ) ,
212+ "MoveCtParameterLast" ) ,
213+ diagnostic ) ;
214+ }
213215
214- context . RegisterCodeFix (
215- CodeAction . Create (
216- moveTitle ,
217- c => MoveCancellationTokenToLastAsync ( context . Document , methodDecl , ctParam , c ) ,
218- equivalenceKey : "MoveCtParameterLast" ) ,
219- diagnostic ) ;
216+ break ;
220217 }
221218 }
222219 }
@@ -249,13 +246,13 @@ private static void RegisterLambdaCancellationTokenFixes(CodeFixContext context,
249246 {
250247 case AsyncMethodConventionsAnalyzer . CancellationTokenMissingId :
251248 {
252- var title = "Add CancellationToken ct parameter" ;
249+ const string title = "Add CancellationToken ct parameter" ;
253250
254251 context . RegisterCodeFix (
255252 CodeAction . Create (
256253 title ,
257254 c => AddCancellationTokenToLambdaAsync ( context . Document , lambdaExpr , c ) ,
258- equivalenceKey : "AddCtParameterToLambda" ) ,
255+ "AddCtParameterToLambda" ) ,
259256 diagnostic ) ;
260257
261258 break ;
@@ -274,7 +271,7 @@ private static void RegisterLambdaCancellationTokenFixes(CodeFixContext context,
274271 CodeAction . Create (
275272 renameTitle ,
276273 c => RenameParameterAsync ( context . Document . Project . Solution , ctParam , "ct" , c ) ,
277- equivalenceKey : "RenameLambdaCtParameter" ) ,
274+ "RenameLambdaCtParameter" ) ,
278275 diagnostic ) ;
279276
280277 break ;
@@ -293,7 +290,7 @@ private static void RegisterLambdaCancellationTokenFixes(CodeFixContext context,
293290 CodeAction . Create (
294291 moveTitle ,
295292 c => MoveCancellationTokenToLastInLambdaAsync ( context . Document , lambdaExpr , ctParam , c ) ,
296- equivalenceKey : "MoveLambdaCtParameterLast" ) ,
293+ "MoveLambdaCtParameterLast" ) ,
297294 diagnostic ) ;
298295
299296 break ;
@@ -416,11 +413,13 @@ private static async Task<Document> MoveCancellationTokenToLastAsync(Document do
416413 for ( var i = 0 ; i < parameters . Count ; i ++ )
417414 {
418415 var p = parameters [ i ] ;
419- if ( string . Equals ( p . Identifier . Text , ctSymbol . Name , StringComparison . Ordinal ) )
416+ if ( ! string . Equals ( p . Identifier . Text , ctSymbol . Name , StringComparison . Ordinal ) )
420417 {
421- ctIndex = i ;
422- break ;
418+ continue ;
423419 }
420+
421+ ctIndex = i ;
422+ break ;
424423 }
425424
426425 if ( ctIndex < 0 || ctIndex == parameters . Count - 1 )
@@ -523,12 +522,8 @@ private static async Task<Document> MoveCancellationTokenToLastInLambdaAsync(Doc
523522 {
524523 var root = await document . GetSyntaxRootAsync ( cancellationToken )
525524 . ConfigureAwait ( false ) ;
526- if ( root is null )
527- {
528- return document ;
529- }
530525
531- if ( lambdaExpr is not ParenthesizedLambdaExpressionSyntax parenthesized )
526+ if ( root is null || lambdaExpr is not ParenthesizedLambdaExpressionSyntax parenthesized )
532527 {
533528 return document ;
534529 }
@@ -539,11 +534,13 @@ private static async Task<Document> MoveCancellationTokenToLastInLambdaAsync(Doc
539534 for ( var i = 0 ; i < parameters . Count ; i ++ )
540535 {
541536 var p = parameters [ i ] ;
542- if ( string . Equals ( p . Identifier . Text , ctSymbol . Name , StringComparison . Ordinal ) )
537+ if ( ! string . Equals ( p . Identifier . Text , ctSymbol . Name , StringComparison . Ordinal ) )
543538 {
544- ctIndex = i ;
545- break ;
539+ continue ;
546540 }
541+
542+ ctIndex = i ;
543+ break ;
547544 }
548545
549546 if ( ctIndex < 0 || ctIndex == parameters . Count - 1 )
0 commit comments