@@ -526,6 +526,53 @@ private enum MyEnum {{ {declarationBody} }}
526526 await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
527527 }
528528
529+ /// <summary>
530+ /// Verifies that <c>new CancellationToken()</c> is replaced by <c>default(CancellationToken)</c> when its used for a default parameter.
531+ /// </summary>
532+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
533+ [ Fact ]
534+ [ WorkItem ( 2740 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2740" ) ]
535+ public async Task VerifyCancellationTokenDefaultParameterAsync ( )
536+ {
537+ var testCode = @"using System.Threading;
538+
539+ public class TestClass
540+ {
541+ public TestClass(CancellationToken cancellationToken = new CancellationToken())
542+ {
543+ }
544+
545+ public void TestMethod(CancellationToken cancellationToken = new CancellationToken())
546+ {
547+ }
548+ }
549+ " ;
550+
551+ var fixedTestCode = @"using System.Threading;
552+
553+ public class TestClass
554+ {
555+ public TestClass(CancellationToken cancellationToken = default(CancellationToken))
556+ {
557+ }
558+
559+ public void TestMethod(CancellationToken cancellationToken = default(CancellationToken))
560+ {
561+ }
562+ }
563+ " ;
564+
565+ DiagnosticResult [ ] expected =
566+ {
567+ this . CSharpDiagnostic ( ) . WithLocation ( 5 , 60 ) ,
568+ this . CSharpDiagnostic ( ) . WithLocation ( 9 , 66 ) ,
569+ } ;
570+
571+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
572+ await this . VerifyCSharpDiagnosticAsync ( fixedTestCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
573+ await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
574+ }
575+
529576 /// <inheritdoc/>
530577 protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
531578 {
0 commit comments