Skip to content

Commit 7b32d7b

Browse files
committed
Add special cases for the SA1129 code fix
* IntPtr.Zero * UIntPtr.Zero * Guid.Empty Fixes #3065
1 parent 37d5f95 commit 7b32d7b

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.ReadabilityRules
66
using System;
77
using System.Collections.Immutable;
88
using System.Composition;
9+
using System.Diagnostics;
910
using System.Linq;
1011
using System.Threading;
1112
using System.Threading.Tasks;
@@ -67,15 +68,37 @@ private static SyntaxNode GetReplacementNode(SyntaxNode node, SemanticModel sema
6768

6869
SyntaxNode replacement;
6970

70-
if (IsType<CancellationToken>(namedTypeSymbol))
71+
if (IsType<CancellationToken>(namedTypeSymbol)
72+
|| namedTypeSymbol?.SpecialType == SpecialType.System_IntPtr
73+
|| namedTypeSymbol?.SpecialType == SpecialType.System_UIntPtr
74+
|| IsType<Guid>(namedTypeSymbol))
7175
{
7276
if (IsDefaultParameterValue(newExpression))
7377
{
7478
replacement = SyntaxFactory.DefaultExpression(newExpression.Type);
7579
}
7680
else
7781
{
78-
replacement = ConstructMemberAccessSyntax(newExpression.Type, nameof(CancellationToken.None));
82+
string fieldName;
83+
if (IsType<CancellationToken>(namedTypeSymbol))
84+
{
85+
fieldName = nameof(CancellationToken.None);
86+
}
87+
else if (namedTypeSymbol.SpecialType == SpecialType.System_IntPtr)
88+
{
89+
fieldName = nameof(IntPtr.Zero);
90+
}
91+
else if (namedTypeSymbol.SpecialType == SpecialType.System_UIntPtr)
92+
{
93+
fieldName = nameof(IntPtr.Zero);
94+
}
95+
else
96+
{
97+
Debug.Assert(IsType<Guid>(namedTypeSymbol), "Assertion failed: IsType<Guid>(namedTypeSymbol)");
98+
fieldName = nameof(Guid.Empty);
99+
}
100+
101+
replacement = ConstructMemberAccessSyntax(newExpression.Type, fieldName);
79102
}
80103
}
81104
else if (IsEnumWithDefaultMember(namedTypeSymbol, out string memberName))

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StyleCop.Analyzers.Test.ReadabilityRules
55
{
6+
using System;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.CodeAnalysis.Testing;
@@ -241,6 +242,9 @@ public T TestMethod2<T>()
241242
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
242243
[Theory]
243244
[InlineData("System.Threading", nameof(CancellationToken), nameof(CancellationToken.None))]
245+
[InlineData("System", nameof(IntPtr), nameof(IntPtr.Zero))]
246+
[InlineData("System", nameof(UIntPtr), nameof(UIntPtr.Zero))]
247+
[InlineData("System", nameof(Guid), nameof(Guid.Empty))]
244248
public async Task VerifySpecialTypeFixUsesSpecialSyntaxAsync(string typeNamespace, string typeName, string fieldName)
245249
{
246250
var testCode = $@"
@@ -279,6 +283,9 @@ public void TestMethod()
279283
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
280284
[Theory]
281285
[InlineData("System.Threading", nameof(CancellationToken), nameof(CancellationToken.None))]
286+
[InlineData("System", nameof(IntPtr), nameof(IntPtr.Zero))]
287+
[InlineData("System", nameof(UIntPtr), nameof(UIntPtr.Zero))]
288+
[InlineData("System", nameof(Guid), nameof(Guid.Empty))]
282289
public async Task VerifySpecialTypeTriviaPreservationAsync(string typeNamespace, string typeName, string fieldName)
283290
{
284291
var testCode = $@"
@@ -318,6 +325,9 @@ public void TestMethod()
318325
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
319326
[Theory]
320327
[InlineData("System.Threading", nameof(CancellationToken), nameof(CancellationToken.None))]
328+
[InlineData("System", nameof(IntPtr), nameof(IntPtr.Zero))]
329+
[InlineData("System", nameof(UIntPtr), nameof(UIntPtr.Zero))]
330+
[InlineData("System", nameof(Guid), nameof(Guid.Empty))]
321331
public async Task VerifyQualifiedSpecialTypeFixUsesFieldSyntaxAsync(string typeNamespace, string typeName, string fieldName)
322332
{
323333
var testCode = $@"
@@ -351,6 +361,9 @@ public void TestMethod()
351361
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
352362
[Theory]
353363
[InlineData(nameof(CancellationToken))]
364+
[InlineData(nameof(IntPtr))]
365+
[InlineData(nameof(UIntPtr))]
366+
[InlineData(nameof(Guid))]
354367
public async Task VerifyCustomSpecialTypeStructIsNotReplacedAsync(string typeName)
355368
{
356369
var testCode = $@"public class TestClass
@@ -394,6 +407,9 @@ private struct {typeName}
394407
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
395408
[Theory]
396409
[InlineData("System.Threading", nameof(CancellationToken), nameof(CancellationToken.None))]
410+
[InlineData("System", nameof(IntPtr), nameof(IntPtr.Zero))]
411+
[InlineData("System", nameof(UIntPtr), nameof(UIntPtr.Zero))]
412+
[InlineData("System", nameof(Guid), nameof(Guid.Empty))]
397413
public async Task VerifyAliasedSpecialTypeUsesFieldSyntaxAsync(string typeNamespace, string typeName, string fieldName)
398414
{
399415
var testCode = $@"
@@ -508,6 +524,9 @@ private enum MyEnum {{ {declarationBody} }}
508524
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
509525
[Theory]
510526
[InlineData("System.Threading", nameof(CancellationToken))]
527+
[InlineData("System", nameof(IntPtr))]
528+
[InlineData("System", nameof(UIntPtr))]
529+
[InlineData("System", nameof(Guid))]
511530
[WorkItem(2740, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2740")]
512531
public async Task VerifySpecialTypeDefaultParameterAsync(string typeNamespace, string typeName)
513532
{

0 commit comments

Comments
 (0)