Skip to content

Commit 95583e3

Browse files
committed
Add tests for IDISP001 when IAsyncDisposable
#199
1 parent 09812b8 commit 95583e3

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace IDisposableAnalyzers.NetCoreTests.IDISP001DisposeCreatedTests
2+
{
3+
using Gu.Roslyn.Asserts;
4+
using Microsoft.CodeAnalysis.Diagnostics;
5+
using NUnit.Framework;
6+
7+
[TestFixture(typeof(LocalDeclarationAnalyzer))]
8+
[TestFixture(typeof(ArgumentAnalyzer))]
9+
[TestFixture(typeof(AssignmentAnalyzer))]
10+
public static class Valid<T>
11+
where T : DiagnosticAnalyzer, new()
12+
{
13+
private static readonly DiagnosticAnalyzer Analyzer = new T();
14+
15+
[Test]
16+
public static void LocalDisposeAsync()
17+
{
18+
var code = @"
19+
namespace N
20+
{
21+
using System.IO;
22+
using System.Threading.Tasks;
23+
24+
public class C
25+
{
26+
public async ValueTask M()
27+
{
28+
var x = File.OpenRead(string.Empty);
29+
await x.DisposeAsync();
30+
}
31+
}
32+
}";
33+
34+
RoslynAssert.Valid(Analyzer, code);
35+
}
36+
37+
[Test]
38+
public static void LocalDisposeAsyncInFinally()
39+
{
40+
var code = @"
41+
namespace N
42+
{
43+
using System.IO;
44+
using System.Threading.Tasks;
45+
46+
public class C
47+
{
48+
public async ValueTask M()
49+
{
50+
var x = File.OpenRead(string.Empty);
51+
try
52+
{
53+
54+
}
55+
finally
56+
{
57+
await x.DisposeAsync();
58+
}
59+
}
60+
}
61+
}";
62+
63+
RoslynAssert.Valid(Analyzer, code);
64+
}
65+
}
66+
}

IDisposableAnalyzers/Helpers/Disposable.Disposes.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,21 +180,14 @@ when recursion.Target(argument) is { } target
180180

181181
bool IsDisposeOrReturnValueDisposed(InvocationExpressionSyntax invocation)
182182
{
183-
if (IsDispose(invocation))
183+
if (DisposeCall.IsMatch(invocation, recursion.SemanticModel, recursion.CancellationToken))
184184
{
185185
return true;
186186
}
187187

188188
return DisposedByReturnValue(invocation, recursion, out var creation) &&
189189
Disposes(creation, recursion);
190190
}
191-
192-
static bool IsDispose(InvocationExpressionSyntax invocation)
193-
{
194-
return invocation is { ArgumentList: { Arguments: { Count: 0 } } } &&
195-
invocation.TryGetMethodName(out var name) &&
196-
name == "Dispose";
197-
}
198191
}
199192
}
200193
}

0 commit comments

Comments
 (0)