@@ -92,6 +92,7 @@ private sealed class AnalyzerContext(Compilation compilation)
92
92
private INamedTypeSymbol ? TaskOfTSymbol { get ; } = compilation . GetBestTypeByMetadataName ( "System.Threading.Tasks.Task`1" ) ;
93
93
private INamedTypeSymbol ? ConfiguredCancelableAsyncEnumerableSymbol { get ; } = compilation . GetBestTypeByMetadataName ( "System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1" ) ;
94
94
private INamedTypeSymbol ? EnumeratorCancellationAttributeSymbol { get ; } = compilation . GetBestTypeByMetadataName ( "System.Runtime.CompilerServices.EnumeratorCancellationAttribute" ) ;
95
+ private INamedTypeSymbol ? XunitTestContextSymbol { get ; } = compilation . GetBestTypeByMetadataName ( "Xunit.TestContext" ) ;
95
96
96
97
private bool HasExplicitCancellationTokenArgument ( IInvocationOperation operation )
97
98
{
@@ -179,7 +180,7 @@ public void AnalyzeInvocation(OperationAnalysisContext context)
179
180
if ( parentMethod is not null && parentMethod . IsOverrideOrInterfaceImplementation ( ) )
180
181
return ;
181
182
182
- context . ReportDiagnostic ( UseAnOverloadThatHasCancellationTokenRule , CreateProperties ( availableCancellationTokens , parameterInfo ) , operation , string . Join ( ", " , availableCancellationTokens ) ) ;
183
+ context . ReportDiagnostic ( UseAnOverloadThatHasCancellationTokenRule , CreateProperties ( availableCancellationTokens , parameterInfo ) , operation ) ;
183
184
}
184
185
}
185
186
@@ -309,7 +310,9 @@ public void AnalyzeLoop(OperationAnalysisContext context)
309
310
310
311
private string [ ] FindCancellationTokens ( IOperation operation , CancellationToken cancellationToken )
311
312
{
313
+
312
314
var availableSymbols = new List < NameAndType > ( ) ;
315
+
313
316
foreach ( var symbol in operation . LookupAvailableSymbols ( cancellationToken ) )
314
317
{
315
318
var symbolType = symbol . GetSymbolType ( ) ;
@@ -319,7 +322,7 @@ private string[] FindCancellationTokens(IOperation operation, CancellationToken
319
322
availableSymbols . Add ( new ( symbol . Name , symbolType ) ) ;
320
323
}
321
324
322
- if ( availableSymbols . Count == 0 )
325
+ if ( availableSymbols . Count == 0 && XunitTestContextSymbol is null )
323
326
return [ ] ;
324
327
325
328
var isInStaticContext = operation . IsInStaticContext ( cancellationToken ) ;
@@ -348,6 +351,11 @@ private string[] FindCancellationTokens(IOperation operation, CancellationToken
348
351
}
349
352
}
350
353
354
+ if ( XunitTestContextSymbol != null )
355
+ {
356
+ paths . Add ( "Xunit.TestContext.Current.CancellationToken" ) ;
357
+ }
358
+
351
359
if ( paths . Count == 0 )
352
360
return [ ] ;
353
361
0 commit comments