Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit 8c33c6c

Browse files
committed
Improved support and tests for out parameters
1 parent da32596 commit 8c33c6c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/Usage/IncludeCancellationParameterUnitTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ private async Task Method2Async() { }
6161
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
6262
}
6363

64+
[Fact]
65+
public async Task TestCancellationTokenIsOutParameterAsync()
66+
{
67+
string testCode = @"
68+
using System.Threading;
69+
using System.Threading.Tasks;
70+
class ClassName
71+
{
72+
public Task MethodAsync(out CancellationToken cancellationToken)
73+
{
74+
cancellationToken = CancellationToken.None;
75+
return Task.FromResult(0);
76+
}
77+
}
78+
";
79+
80+
var expected = this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(6, 17);
81+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
82+
}
83+
6484
[Fact]
6585
public async Task TestCancellationTokenInStructureAsync()
6686
{
@@ -165,6 +185,31 @@ struct Context
165185
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
166186
}
167187

188+
[Fact]
189+
public async Task TestContextStructureIsOutParameterAsync()
190+
{
191+
string testCode = @"
192+
using System.Threading;
193+
using System.Threading.Tasks;
194+
class ClassName
195+
{
196+
public Task MethodAsync(out Context context)
197+
{
198+
context = default(Context);
199+
return Task.FromResult(0);
200+
}
201+
}
202+
203+
struct Context
204+
{
205+
public CancellationToken CancellationToken { get; }
206+
}
207+
";
208+
209+
var expected = this.CSharpDiagnostic().WithArguments("MethodAsync").WithLocation(6, 17);
210+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
211+
}
212+
168213
[Fact]
169214
public async Task TestCancellationTokenInInterfaceAsync()
170215
{

AsyncUsageAnalyzers/AsyncUsageAnalyzers/Usage/IncludeCancellationParameterAnalyzer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public void HandleMethodDeclaration(SymbolAnalysisContext context)
100100

101101
foreach (var parameterSymbol in symbol.Parameters)
102102
{
103+
if (parameterSymbol.RefKind == RefKind.Out)
104+
{
105+
continue;
106+
}
107+
103108
INamedTypeSymbol parameterType = parameterSymbol.Type as INamedTypeSymbol;
104109
if (this.cancellationTokenType.Equals(parameterType))
105110
{

0 commit comments

Comments
 (0)