Skip to content

Commit 4fc95bf

Browse files
committed
BlockingCallInsideAsync Analyzer: Stop suggesting async calls from non-system assemblies
1 parent 3a12f76 commit 4fc95bf

File tree

4 files changed

+38
-132
lines changed

4 files changed

+38
-132
lines changed

AsyncFixer.Test/BlockingCallInsideAsyncTests.cs

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ async Task foo()
167167
}
168168

169169
[Fact]
170-
public void BlockingCallInsideAsyncTest5()
170+
public void NoWarn_BlockingCallInsideAsyncTest5()
171171
{
172172
var test = @"
173173
using System;
@@ -191,83 +191,7 @@ public Task<int> booAsync(int a)
191191
}
192192
}";
193193

194-
var expected = new DiagnosticResult { Id = DiagnosticIds.BlockingCallInsideAsync };
195-
VerifyCSharpDiagnostic(test, expected);
196-
197-
var fixtest = @"
198-
using System;
199-
using System.Threading.Tasks;
200-
201-
class Program
202-
{
203-
async Task foo()
204-
{
205-
// comment
206-
await this.booAsync(0);
207-
// end comment
208-
}
209-
public int boo(int a)
210-
{
211-
return 3;
212-
}
213-
public Task<int> booAsync(int a)
214-
{
215-
return Task.Run(()=>4);
216-
}
217-
}";
218-
219-
VerifyCSharpFix(test, fixtest);
220-
}
221-
222-
[Fact]
223-
public void BlockingCallInsideAsyncTest6()
224-
{
225-
var test = @"
226-
using System;
227-
using System.Threading.Tasks;
228-
229-
class Program
230-
{
231-
async Task foo()
232-
{
233-
// comment
234-
boo(0);
235-
}
236-
public int boo(int a)
237-
{
238-
return 3;
239-
}
240-
public async Task<int> booAsync(int a)
241-
{
242-
return a;
243-
}
244-
}";
245-
246-
var expected = new DiagnosticResult { Id = DiagnosticIds.BlockingCallInsideAsync };
247-
VerifyCSharpDiagnostic(test, expected);
248-
249-
var fixtest = @"
250-
using System;
251-
using System.Threading.Tasks;
252-
253-
class Program
254-
{
255-
async Task foo()
256-
{
257-
// comment
258-
await booAsync(0);
259-
}
260-
public int boo(int a)
261-
{
262-
return 3;
263-
}
264-
public async Task<int> booAsync(int a)
265-
{
266-
return a;
267-
}
268-
}";
269-
270-
VerifyCSharpFix(test, fixtest);
194+
VerifyCSharpDiagnostic(test);
271195
}
272196

273197
[Fact]
@@ -302,54 +226,6 @@ async Task foo()
302226
VerifyCSharpFix(test, fixtest);
303227
}
304228

305-
[Fact]
306-
public void BlockingCallInsideAsyncTest8()
307-
{
308-
var test = @"
309-
using System;
310-
using System.Threading.Tasks;
311-
312-
class Program
313-
{
314-
async Task foo()
315-
{
316-
boo(0);
317-
}
318-
int boo(int a)
319-
{
320-
return a;
321-
}
322-
async Task<int> booAsync(int a)
323-
{
324-
return 3;
325-
}
326-
}";
327-
var expected = new DiagnosticResult { Id = DiagnosticIds.BlockingCallInsideAsync };
328-
VerifyCSharpDiagnostic(test, expected);
329-
330-
var fixtest = @"
331-
using System;
332-
using System.Threading.Tasks;
333-
334-
class Program
335-
{
336-
async Task foo()
337-
{
338-
await booAsync(0);
339-
}
340-
int boo(int a)
341-
{
342-
return a;
343-
}
344-
async Task<int> booAsync(int a)
345-
{
346-
return 3;
347-
}
348-
}";
349-
350-
VerifyCSharpFix(test, fixtest);
351-
}
352-
353229
[Fact]
354230
public void BlockingCallInsideAsyncTest9()
355231
{

AsyncFixer.Test/NestedTaskToOuterTaskTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ TaskCanceledException foo()
100100
VerifyCSharpDiagnostic(test);
101101
}
102102

103+
[Fact]
104+
public void GenericTaskType()
105+
{
106+
var test = @"
107+
using System.Threading.Tasks;
108+
109+
class Program
110+
{
111+
async void main()
112+
{
113+
await Task.Factory.StartNew(() => foo());
114+
}
115+
116+
Task<int> foo()
117+
{
118+
return Task.FromResult(3);
119+
}
120+
}";
121+
122+
var expected = new DiagnosticResult { Id = DiagnosticIds.NestedTaskToOuterTask };
123+
VerifyCSharpDiagnostic(test, expected);
124+
}
125+
103126
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
104127
{
105128
return new NestedTaskToOuterTaskAnalyzer();

AsyncFixer/BlockingCallInsideAsync/BlockingCallInsideAsyncAnalyzer.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,21 @@ private void AnalyzeMemberAccess(SyntaxNodeAnalysisContext context, MethodDeclar
125125

126126
private static string DetectSynchronousUsages(Location location, IMethodSymbol methodCallSymbol, SemanticModel semanticModel)
127127
{
128-
var methodName = methodCallSymbol.Name;
129-
130-
var typeName = methodCallSymbol.ContainingType.Name;
131-
132128
if (methodCallSymbol.ContainingType == null)
133129
{
134130
return null;
135131
}
136132

133+
if (!methodCallSymbol.ContainingAssembly.ToDisplayString().StartsWith("System.", StringComparison.OrdinalIgnoreCase))
134+
{
135+
// Only look at the symbols from the System assemblies.
136+
return null;
137+
}
138+
139+
var methodName = methodCallSymbol.Name;
140+
141+
var typeName = methodCallSymbol.ContainingType.Name;
142+
137143
if (typeName == "MemoryStream")
138144
{
139145
// Do not replace the synchronous ones under MemoryStream.

AsyncFixer/Helpers.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ public static bool IsTaskCreationMethod(this IMethodSymbol symbol)
2121

2222
public static bool IsTask(this ITypeSymbol type)
2323
{
24-
return type.ToDisplayString().StartsWith("System.Threading.Tasks.Task", StringComparison.OrdinalIgnoreCase);
24+
return type.ContainingNamespace.ToDisplayString() == "System.Threading.Tasks" &&
25+
type.Name == "Task";
2526
}
2627

2728
public static bool ReturnTask(this IMethodSymbol symbol)
2829
{
29-
return !symbol.ReturnsVoid && symbol.ReturnType.ToString().StartsWith("System.Threading.Tasks.Task", StringComparison.OrdinalIgnoreCase);
30+
return !symbol.ReturnsVoid && symbol.ReturnType.IsTask();
3031
}
3132

3233
public static bool IsAsync(this MethodDeclarationSyntax method)

0 commit comments

Comments
 (0)