File tree Expand file tree Collapse file tree
StyleCop.Analyzers.CodeFixes/ReadabilityRules
StyleCop.Analyzers.Test/ReadabilityRules Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,12 +58,32 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5858 private static SyntaxNode GetReplacementNode ( SyntaxNode node )
5959 {
6060 var newExpression = ( ObjectCreationExpressionSyntax ) node ;
61+ var identifierName = ( newExpression . Type as IdentifierNameSyntax )
62+ ? . Identifier . Text ;
6163
62- return SyntaxFactory . DefaultExpression ( newExpression . Type )
64+ SyntaxNode replacement = null ;
65+ if ( identifierName . Equals ( nameof ( CancellationToken ) , System . StringComparison . OrdinalIgnoreCase ) )
66+ {
67+ replacement = GetCancellationTokenNoneSyntax ( ) ;
68+ }
69+ else
70+ {
71+ replacement = SyntaxFactory . DefaultExpression ( newExpression . Type ) ;
72+ }
73+
74+ return replacement
6375 . WithLeadingTrivia ( newExpression . GetLeadingTrivia ( ) )
6476 . WithTrailingTrivia ( newExpression . GetTrailingTrivia ( ) ) ;
6577 }
6678
79+ private static SyntaxNode GetCancellationTokenNoneSyntax ( )
80+ {
81+ return SyntaxFactory . MemberAccessExpression (
82+ SyntaxKind . SimpleMemberAccessExpression ,
83+ SyntaxFactory . IdentifierName ( nameof ( CancellationToken ) ) ,
84+ SyntaxFactory . IdentifierName ( nameof ( CancellationToken . None ) ) ) ;
85+ }
86+
6787 private class FixAll : DocumentBasedFixAllProvider
6888 {
6989 public static FixAllProvider Instance { get ; } =
Original file line number Diff line number Diff line change @@ -237,6 +237,47 @@ public T TestMethod2<T>()
237237 await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
238238 }
239239
240+ /// <summary>
241+ /// Verifies that <c>new CancellationTokenI()</c> is replaced by <c>CancellationToken.None</c>.
242+ /// </summary>
243+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
244+ [ Fact ]
245+ public async Task VerifyCancellationTokenFixUsesNoneSyntaxAsync ( )
246+ {
247+ var testCode = @"
248+ using System.Threading;
249+
250+ public class TestClass
251+ {
252+ public void TestMethod()
253+ {
254+ var ct = new CancellationToken();
255+ }
256+ }
257+ " ;
258+
259+ var fixedTestCode = @"
260+ using System.Threading;
261+
262+ public class TestClass
263+ {
264+ public void TestMethod()
265+ {
266+ var ct = CancellationToken.None;
267+ }
268+ }
269+ " ;
270+
271+ DiagnosticResult [ ] expected =
272+ {
273+ this . CSharpDiagnostic ( ) . WithLocation ( 8 , 18 ) ,
274+ } ;
275+
276+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
277+ await this . VerifyCSharpDiagnosticAsync ( fixedTestCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
278+ await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
279+ }
280+
240281 /// <inheritdoc/>
241282 protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
242283 {
You can’t perform that action at this time.
0 commit comments