Skip to content

Commit 5f8e6bb

Browse files
committed
Add Microsoft.CodeAnalysis.FxCopAnalyzers
1 parent fb66a99 commit 5f8e6bb

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

FollowingFileStream.ConsoleTestTool/Program.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ static class Program
99
private const string InputPath = "source.txt";
1010
private const string OutputPath = "destination.txt";
1111

12-
static async Task Main(string[] args)
12+
static async Task Main()
1313
{
14-
Console.WriteLine("Hello World!");
15-
var input = WriteInput();
16-
await CopyToOutput();
14+
var input = WriteInput().ConfigureAwait(false);
15+
await CopyToOutput().ConfigureAwait(false);
1716
await input;
1817
}
1918

@@ -22,20 +21,20 @@ private static async Task WriteInput()
2221
using (var sw = new StreamWriter(new FileStream(InputPath, FileMode.Create, FileAccess.Write, FileShare.Read)))
2322
using (var sr = new StreamReader(Console.OpenStandardInput()))
2423
{
25-
await sr.CopyToAsync(sw, stopOn:"quit");
24+
await sr.CopyToAsync(sw, stopOn:"quit").ConfigureAwait(false);
2625
}
2726
}
2827
private static async Task CopyToOutput()
2928
{
3029
using (var source = new FollowingFileStream(InputPath))
3130
using (var destination = new FileStream(OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
3231
{
33-
await source.CopyToAsync(destination);
32+
await source.CopyToAsync(destination).ConfigureAwait(false);
3433
}
3534
}
3635
}
3736

38-
static public class TextReaderExtensions
37+
static class TextReaderExtensions
3938
{
4039
public static async Task CopyToAsync(this TextReader reader, TextWriter writer, string stopOn)
4140
{

FollowingFileStream/AsyncStream.cs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Manandre.IO
1111
/// <summary>
1212
///
1313
/// </summary>
14-
#pragma warning disable S3881
14+
#pragma warning disable S3881
1515
public abstract class AsyncStream : Stream
1616
{
1717
#if !NETSTANDARD1_3
@@ -442,50 +442,56 @@ public override void SetLength(long value)
442442
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
443443
{
444444
var read = 0;
445-
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);
446-
try
445+
using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token))
447446
{
448-
using (await locker.LockAsync(linkedCts.Token))
447+
try
449448
{
450-
read = await _stream.ReadAsync(buffer, offset, count, linkedCts.Token);
449+
using (await locker.LockAsync(linkedCts.Token))
450+
{
451+
read = await _stream.ReadAsync(buffer, offset, count, linkedCts.Token).ConfigureAwait(false);
452+
}
453+
}
454+
catch (OperationCanceledException)
455+
{
456+
cancellationToken.ThrowIfCancellationRequested();
451457
}
452-
}
453-
catch (OperationCanceledException)
454-
{
455-
cancellationToken.ThrowIfCancellationRequested();
456458
}
457459
return read;
458460
}
459461

460462
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
461463
{
462-
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);
463-
try
464+
using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token))
464465
{
465-
using (await locker.LockAsync(linkedCts.Token))
466+
try
466467
{
467-
await _stream.WriteAsync(buffer, offset, count, linkedCts.Token);
468+
using (await locker.LockAsync(linkedCts.Token))
469+
{
470+
await _stream.WriteAsync(buffer, offset, count, linkedCts.Token).ConfigureAwait(false);
471+
}
472+
}
473+
catch (OperationCanceledException)
474+
{
475+
cancellationToken.ThrowIfCancellationRequested();
468476
}
469-
}
470-
catch (OperationCanceledException)
471-
{
472-
cancellationToken.ThrowIfCancellationRequested();
473477
}
474478
}
475479

476480
public override async Task FlushAsync(CancellationToken cancellationToken)
477481
{
478-
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);
479-
try
482+
using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token))
480483
{
481-
using (await locker.LockAsync(linkedCts.Token))
484+
try
482485
{
483-
await _stream.FlushAsync(linkedCts.Token);
486+
using (await locker.LockAsync(linkedCts.Token))
487+
{
488+
await _stream.FlushAsync(linkedCts.Token).ConfigureAwait(false);
489+
}
490+
}
491+
catch (OperationCanceledException)
492+
{
493+
cancellationToken.ThrowIfCancellationRequested();
484494
}
485-
}
486-
catch (OperationCanceledException)
487-
{
488-
cancellationToken.ThrowIfCancellationRequested();
489495
}
490496
}
491497

@@ -496,7 +502,7 @@ protected override async ValueTask DisposeAsync(bool disposing)
496502
{
497503
if (disposed)
498504
return;
499-
505+
500506
try
501507
{
502508
// Explicitly pick up a potentially methodimpl'ed DisposeAsync
@@ -507,6 +513,7 @@ protected override async ValueTask DisposeAsync(bool disposing)
507513
{
508514
await ((IAsyncDisposable)_stream).DisposeAsync();
509515
}
516+
cts.Dispose();
510517
}
511518
}
512519
finally
@@ -531,6 +538,7 @@ protected override void Dispose(bool disposing)
531538
{
532539
((IDisposable)_stream).Dispose();
533540
}
541+
cts.Dispose();
534542

535543
}
536544
}
@@ -544,7 +552,7 @@ protected override void Dispose(bool disposing)
544552
}
545553
}
546554

547-
#pragma warning restore S3881
555+
#pragma warning restore S3881
548556

549557
/// <summary>
550558
/// AsyncStream class extensions

FollowingFileStream/FollowingFileStream.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
237237
int read = 0;
238238
do
239239
{
240-
read = await fileStream.ReadAsync(buffer, offset, count, cancellationToken);
240+
read = await fileStream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
241241
}
242-
while (read == 0 && await RetryNeededAsync());
242+
while (read == 0 && await RetryNeededAsync().ConfigureAwait(false));
243243

244244
// In case the filestream has been written and closed between the last read operation
245245
// and the IsFileLockedForWriting() check
246246
if (read == 0)
247247
{
248-
read = await fileStream.ReadAsync(buffer, offset, count, cancellationToken);
248+
read = await fileStream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
249249
}
250250

251251
TotalTime = 0;
@@ -348,6 +348,7 @@ protected override async ValueTask DisposeAsync(bool disposing)
348348
{
349349
cts.Cancel();
350350
await fileStream.DisposeAsync();
351+
cts.Dispose();
351352
}
352353
}
353354
finally
@@ -373,6 +374,7 @@ protected override void Dispose(bool disposing)
373374
{
374375
cts.Cancel();
375376
fileStream.Dispose();
377+
cts.Dispose();
376378
}
377379

378380
disposed = true;

FollowingFileStream/FollowingFileStream.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<ItemGroup>
2020
<PackageReference Include="Nito.AsyncEx" Version="5.0.0"/>
2121
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19367-01" PrivateAssets="All"/>
22+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.5"/>
2223
</ItemGroup>
23-
</Project>
24+
</Project>

0 commit comments

Comments
 (0)