Skip to content

Commit f9496ab

Browse files
committed
Enable nullable C# checks
1 parent 6f94922 commit f9496ab

File tree

7 files changed

+24
-25
lines changed

7 files changed

+24
-25
lines changed

FollowingFileStream.ConsoleTestTool/FollowingFileStream.ConsoleTestTool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<OutputType>Exe</OutputType>
99
<TargetFrameworks>netcoreapp3.0;netcoreapp2.2</TargetFrameworks>
1010
<LangVersion>8.0</LangVersion>
11+
<nullable>enable</nullable>
1112
<IsPackable>false</IsPackable>
1213
<noWarn>SA0001</noWarn>
1314
</PropertyGroup>

FollowingFileStream.ConsoleTestTool/TextReaderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal static class TextReaderExtensions
2424
/// <returns>A task representing the copy operation.</returns>
2525
public static async Task CopyToAsync(this TextReader reader, TextWriter writer, string stopOn)
2626
{
27-
string line;
27+
string? line;
2828
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != stopOn)
2929
{
3030
await writer.WriteLineAsync(line).ConfigureAwait(false);

FollowingFileStream.Tests/AsyncStreamTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ public void ASRead()
3030
var expected = 42;
3131
sut.Setup(x => x.ReadAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
3232
.ReturnsAsync(expected);
33-
var read = sut.Object.Read(null, 0, 0);
33+
var read = sut.Object.Read(null!, 0, 0);
3434
Assert.AreEqual(expected, read);
3535

36-
read = sut.Object.EndRead(sut.Object.BeginRead(null, 0, 0, null, null));
36+
read = sut.Object.EndRead(sut.Object.BeginRead(null!, 0, 0, null!, null!));
3737
Assert.AreEqual(expected, read);
3838
}
3939

@@ -46,10 +46,10 @@ public void ASWrite()
4646
var sut = new Mock<AsyncStream>() { CallBase = true };
4747
sut.Setup(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
4848
.Verifiable();
49-
sut.Object.Write(null, 0, 0);
49+
sut.Object.Write(null!, 0, 0);
5050
sut.Verify(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Once);
5151

52-
sut.Object.EndWrite(sut.Object.BeginWrite(null, 0, 0, null, null));
52+
sut.Object.EndWrite(sut.Object.BeginWrite(null!, 0, 0, null!, null!));
5353
sut.Verify(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
5454
}
5555

@@ -82,7 +82,7 @@ public void ASDispose()
8282
[TestMethod]
8383
public void ASSynchronized()
8484
{
85-
Assert.ThrowsException<ArgumentNullException>(() => AsyncStream.Synchronized(null));
85+
Assert.ThrowsException<ArgumentNullException>(() => AsyncStream.Synchronized(null!));
8686
var sut = new Mock<AsyncStream>() { CallBase = true };
8787
var synchronized = AsyncStream.Synchronized(sut.Object);
8888
Assert.IsNotNull(synchronized);
@@ -156,23 +156,23 @@ public void ASSynchronized()
156156
// Read
157157
sut.Setup(x => x.ReadAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
158158
.ReturnsAsync(expected2);
159-
var read = synchronized.ReadAsync(null, 0, 0).Result;
159+
var read = synchronized.ReadAsync(null!, 0, 0).Result;
160160
Assert.AreEqual(expected2, read);
161-
read = synchronized.Read(null, 0, 0);
161+
read = synchronized.Read(null!, 0, 0);
162162
Assert.AreEqual(expected2, read);
163-
read = synchronized.EndRead(synchronized.BeginRead(null, 0, 0, null, null));
163+
read = synchronized.EndRead(synchronized.BeginRead(null!, 0, 0, null!, null!));
164164
Assert.AreEqual(expected2, read);
165165

166166
// Write
167167
sut.Setup(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
168168
.Verifiable();
169-
synchronized.WriteAsync(null, 0, 0).Wait();
169+
synchronized.WriteAsync(null!, 0, 0).Wait();
170170
sut.Verify(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Once);
171-
synchronized.Write(null, 0, 0);
171+
synchronized.Write(null!, 0, 0);
172172
sut.Verify(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
173-
synchronized.EndWrite(synchronized.BeginWrite(null, 0, 0, null, null));
173+
synchronized.EndWrite(synchronized.BeginWrite(null!, 0, 0, null!, null!));
174174
sut.Verify(x => x.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Exactly(3));
175-
Assert.ThrowsExceptionAsync<OperationCanceledException>(() => synchronized.WriteAsync(null, 0, 0, new CancellationToken(true)));
175+
Assert.ThrowsExceptionAsync<OperationCanceledException>(() => synchronized.WriteAsync(null!, 0, 0, new CancellationToken(true)));
176176

177177
// Async
178178
sut.Setup(x => x.FlushAsync(It.IsAny<CancellationToken>())).Verifiable();

FollowingFileStream.Tests/FollowingFileStream.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<TargetFrameworks>netcoreapp3.0;netcoreapp2.2</TargetFrameworks>
44
<LangVersion>8.0</LangVersion>
5+
<nullable>enable</nullable>
56
<IsPackable>false</IsPackable>
67
<noWarn>SA0001</noWarn>
78
</PropertyGroup>

FollowingFileStream.Tests/FollowingFileStreamTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public FollowingFileStreamTest()
3737
public void FFSNullPath()
3838
{
3939
Assert.ThrowsException<ArgumentNullException>(
40-
() => new FollowingFileStream(null),
40+
() => new FollowingFileStream(null!),
4141
"exception expected on null path");
4242
}
4343

@@ -109,10 +109,10 @@ public void FFSProperties(bool async)
109109
public void FFSModification()
110110
{
111111
using var ffs = new FollowingFileStream(this.inputFilePath);
112-
Assert.ThrowsException<NotSupportedException>(() => ffs.Write(null, 0, 0));
113-
Assert.ThrowsException<NotSupportedException>(() => ffs.WriteAsync(null, 0, 0));
112+
Assert.ThrowsException<NotSupportedException>(() => ffs.Write(null!, 0, 0));
113+
Assert.ThrowsException<NotSupportedException>(() => ffs.WriteAsync(null!, 0, 0));
114114
Assert.ThrowsException<NotSupportedException>(() => ffs.WriteByte(0x0));
115-
Assert.ThrowsException<NotSupportedException>(() => ffs.BeginWrite(null, 0, 0, null, null));
115+
Assert.ThrowsException<NotSupportedException>(() => ffs.BeginWrite(null!, 0, 0, null!, null!));
116116

117117
Assert.ThrowsException<NotSupportedException>(() => ffs.SetLength(0));
118118
Assert.ThrowsException<NotSupportedException>(() => ffs.Flush());
@@ -142,7 +142,7 @@ public void FFSRead(bool async)
142142
Assert.AreEqual(expected, System.Text.Encoding.Default.GetString(bytes));
143143

144144
ffs.Position = 0;
145-
Assert.AreEqual(expected.Length, ffs.EndRead(ffs.BeginRead(bytes, 0, bytes.Length, null, null)));
145+
Assert.AreEqual(expected.Length, ffs.EndRead(ffs.BeginRead(bytes, 0, bytes.Length, null!, null!)));
146146
Assert.AreEqual(expected, System.Text.Encoding.Default.GetString(bytes));
147147

148148
var cts = new CancellationTokenSource();

FollowingFileStream/FollowingFileStream.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,11 @@ private async Task<bool> RetryNeededAsync()
440440
/// <returns> true if the file is locked for writing; false, otherwise.</returns>
441441
private bool IsFileLockedForWriting()
442442
{
443-
FileStream stream = null;
444-
445443
try
446444
{
447-
stream = new FileStream(this.fileStream.Name, FileMode.Open, FileAccess.Write, FileShare.Read);
445+
using (new FileStream(this.fileStream.Name, FileMode.Open, FileAccess.Write, FileShare.Read))
446+
{
447+
}
448448
}
449449
catch (IOException)
450450
{
@@ -454,10 +454,6 @@ private bool IsFileLockedForWriting()
454454
// or does not exist (has already been processed)
455455
return true;
456456
}
457-
finally
458-
{
459-
stream?.Dispose();
460-
}
461457

462458
// file is not locked
463459
return false;

FollowingFileStream/FollowingFileStream.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<OutputType>Library</OutputType>
44
<TargetFrameworks>netstandard2.1;netstandard2.0;netstandard1.3</TargetFrameworks>
55
<LangVersion>8.0</LangVersion>
6+
<nullable>enable</nullable>
67
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
78
</PropertyGroup>
89
<PropertyGroup>

0 commit comments

Comments
 (0)