Skip to content

Commit 7aff560

Browse files
committed
Use CancellationToken.None in the code fix for SA1129
1 parent acfd3f3 commit 7aff560

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1129CodeFixProvider.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff 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; } =

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1129UnitTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff 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
{

0 commit comments

Comments
 (0)