File tree Expand file tree Collapse file tree
IDisposableAnalyzers.NetCoreTests/IDISP003DisposeBeforeReassigningTests
ValidCode.NetCore/AsyncDisposable Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ namespace IDisposableAnalyzers . NetCoreTests . IDISP003DisposeBeforeReassigningTests
2+ {
3+ using Gu . Roslyn . Asserts ;
4+ using Microsoft . CodeAnalysis . Diagnostics ;
5+ using NUnit . Framework ;
6+
7+ public static class Valid
8+ {
9+ private static readonly DiagnosticAnalyzer Analyzer = new AssignmentAnalyzer ( ) ;
10+
11+ [ Test ]
12+ public static void FieldDisposeAsyncInDisposeAsync ( )
13+ {
14+ var code = @"
15+ namespace N
16+ {
17+ using System;
18+ using System.Threading;
19+ using System.Threading.Tasks;
20+
21+ public class C : IAsyncDisposable
22+ {
23+ private Timer _timer;
24+
25+ public async Task ResetTimerAsync()
26+ {
27+ if (_timer != null)
28+ {
29+ await _timer.DisposeAsync();
30+ _timer = null; // Warns with IDISP003: Dispose previous before re-assigning.
31+ }
32+ }
33+
34+ public async ValueTask DisposeAsync()
35+ {
36+ await _timer.DisposeAsync();
37+ }
38+ }
39+ }" ;
40+ RoslynAssert . Valid ( Analyzer , code ) ;
41+ }
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ namespace ValidCode . NetCore . AsyncDisposable
2+ {
3+ using System ;
4+ using System . Threading ;
5+ using System . Threading . Tasks ;
6+
7+ public class Issue199 : IAsyncDisposable
8+ {
9+ private Timer _timer ;
10+
11+ public async Task ResetTimerAsync ( )
12+ {
13+ if ( _timer != null )
14+ {
15+ await _timer . DisposeAsync ( ) ;
16+ _timer = null ; // Warns with IDISP003: Dispose previous before re-assigning.
17+ }
18+ }
19+
20+ public async ValueTask DisposeAsync ( )
21+ {
22+ await _timer . DisposeAsync ( ) ;
23+ }
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments