Skip to content

Commit

Permalink
.NET 9: fix CA2022
Browse files Browse the repository at this point in the history
see dotnet/roslyn-analyzers#7208

Most of these could be safely ignored because of the following assert.
As far as I can see, The SftpFileStream.Read() implementation guarentees
that the specified number of bytes is read anyway.
  • Loading branch information
mus65 committed Jun 15, 2024
1 parent f2009ff commit 784d484
Show file tree
Hide file tree
Showing 45 changed files with 103 additions and 95 deletions.
71 changes: 36 additions & 35 deletions test/Renci.SshNet.IntegrationTests/SftpTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void GetBytes()
Assert.AreEqual((uint)size, sshDataStream.ReadUInt32());

var actualData = new byte[size];
sshDataStream.Read(actualData, 0, size);
_ = sshDataStream.Read(actualData, 0, size);
Assert.IsTrue(actualData.SequenceEqual(data.Take(offset, size)));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ public void GetBytes()
Assert.AreEqual((uint)target.ChannelType.Length, actualChannelTypeLength);

var actualChannelType = new byte[actualChannelTypeLength];
sshDataStream.Read(actualChannelType, 0, (int)actualChannelTypeLength);
_ = sshDataStream.Read(actualChannelType, 0, (int)actualChannelTypeLength);
Assert.IsTrue(target.ChannelType.SequenceEqual(actualChannelType));

Assert.AreEqual(localChannelNumber, sshDataStream.ReadUInt32());
Assert.AreEqual(initialWindowSize, sshDataStream.ReadUInt32());
Assert.AreEqual(maximumPacketSize, sshDataStream.ReadUInt32());

var actualInfo = new byte[infoBytes.Length];
sshDataStream.Read(actualInfo, 0, actualInfo.Length);
_ = sshDataStream.Read(actualInfo, 0, actualInfo.Length);
Assert.IsTrue(infoBytes.SequenceEqual(actualInfo));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void GetBytes()
Assert.AreEqual((uint)_data.Length, sshDataStream.ReadUInt32());

var actualData = new byte[_data.Length];
sshDataStream.Read(actualData, 0, actualData.Length);
_ = sshDataStream.Read(actualData, 0, actualData.Length);
Assert.IsTrue(_data.SequenceEqual(actualData));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ public void GetBytes()
Assert.AreEqual((uint)_nameBytes.Length, sshDataStream.ReadUInt32());

var actualNameBytes = new byte[_nameBytes.Length];
sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
_ = sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
Assert.IsTrue(_nameBytes.SequenceEqual(actualNameBytes));

Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());

var actualHandle = new byte[_handle.Length];
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
_ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
Assert.IsTrue(_handle.SequenceEqual(actualHandle));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ public void GetBytes()
Assert.AreEqual((uint)_nameBytes.Length, sshDataStream.ReadUInt32());

var actualNameBytes = new byte[_nameBytes.Length];
sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
_ = sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
Assert.IsTrue(_nameBytes.SequenceEqual(actualNameBytes));

Assert.AreEqual((uint)_oldPathBytes.Length, sshDataStream.ReadUInt32());

var actualOldPath = new byte[_oldPathBytes.Length];
sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
_ = sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
Assert.IsTrue(_oldPathBytes.SequenceEqual(actualOldPath));

Assert.AreEqual((uint)_newPathBytes.Length, sshDataStream.ReadUInt32());

var actualNewPath = new byte[_newPathBytes.Length];
sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
_ = sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
Assert.IsTrue(_newPathBytes.SequenceEqual(actualNewPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ public void GetBytes()
Assert.AreEqual((uint)_nameBytes.Length, sshDataStream.ReadUInt32());

var actualNameBytes = new byte[_nameBytes.Length];
sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
_ = sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
Assert.IsTrue(_nameBytes.SequenceEqual(actualNameBytes));

Assert.AreEqual((uint)_oldPathBytes.Length, sshDataStream.ReadUInt32());

var actualOldPath = new byte[_oldPathBytes.Length];
sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
_ = sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
Assert.IsTrue(_oldPathBytes.SequenceEqual(actualOldPath));

Assert.AreEqual((uint)_newPathBytes.Length, sshDataStream.ReadUInt32());

var actualNewPath = new byte[_newPathBytes.Length];
sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
_ = sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
Assert.IsTrue(_newPathBytes.SequenceEqual(actualNewPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ public void GetBytes()
Assert.AreEqual((uint)_nameBytes.Length, sshDataStream.ReadUInt32());

var actualNameBytes = new byte[_nameBytes.Length];
sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
_ = sshDataStream.Read(actualNameBytes, 0, actualNameBytes.Length);
Assert.IsTrue(_nameBytes.SequenceEqual(actualNameBytes));

Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());

var actualPath = new byte[_pathBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void GetBytes()

Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
var actualHandle = new byte[_handle.Length];
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
_ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
Assert.IsTrue(_handle.SequenceEqual(actualHandle));

Assert.AreEqual(_offset, sshDataStream.ReadUInt64());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void GetBytes()

Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
var actualHandle = new byte[_handle.Length];
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
_ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
Assert.IsTrue(_handle.SequenceEqual(actualHandle));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public void GetBytes()

Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
var actualHandle = new byte[_handle.Length];
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
_ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
Assert.IsTrue(_handle.SequenceEqual(actualHandle));

var actualAttributes = new byte[_attributesBytes.Length];
sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
_ = sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
Assert.IsTrue(_attributesBytes.SequenceEqual(actualAttributes));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void GetBytes()

Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
var actualHandle = new byte[_handle.Length];
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
_ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
Assert.IsTrue(_handle.SequenceEqual(actualHandle));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void GetBytes()

Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
var actualPath = new byte[_pathBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public void GetBytes()

Assert.AreEqual((uint)_newLinkPathBytes.Length, sshDataStream.ReadUInt32());
var actualNewLinkPath = new byte[_newLinkPathBytes.Length];
sshDataStream.Read(actualNewLinkPath, 0, actualNewLinkPath.Length);
_ = sshDataStream.Read(actualNewLinkPath, 0, actualNewLinkPath.Length);
Assert.IsTrue(_newLinkPathBytes.SequenceEqual(actualNewLinkPath));

Assert.AreEqual((uint)_existingPathBytes.Length, sshDataStream.ReadUInt32());
var actualExistingPath = new byte[_existingPathBytes.Length];
sshDataStream.Read(actualExistingPath, 0, actualExistingPath.Length);
_ = sshDataStream.Read(actualExistingPath, 0, actualExistingPath.Length);
Assert.IsTrue(_existingPathBytes.SequenceEqual(actualExistingPath));

Assert.AreEqual(1, sshDataStream.ReadByte());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public void GetBytes()

Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
var actualPath = new byte[_pathBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

var actualAttributes = new byte[_attributesBytes.Length];
sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
_ = sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
Assert.IsTrue(_attributesBytes.SequenceEqual(actualAttributes));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void GetBytes()

Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
var actualPath = new byte[_pathBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ public void GetBytes()

Assert.AreEqual((uint)_filenameBytes.Length, sshDataStream.ReadUInt32());
var actualPath = new byte[_filenameBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_filenameBytes.SequenceEqual(actualPath));

Assert.AreEqual((uint)_flags, sshDataStream.ReadUInt32());

var actualAttributes = new byte[_attributesBytes.Length];
sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
_ = sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
Assert.IsTrue(_attributesBytes.SequenceEqual(actualAttributes));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void GetBytes()

Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
var actualHandle = new byte[_handle.Length];
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
_ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
Assert.IsTrue(_handle.SequenceEqual(actualHandle));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void GetBytes()

Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
var actualPath = new byte[_pathBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void GetBytes()

Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
var actualHandle = new byte[_handle.Length];
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
_ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
Assert.IsTrue(_handle.SequenceEqual(actualHandle));

Assert.AreEqual(_offset, sshDataStream.ReadUInt64());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void GetBytes()

Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
var actualPath = new byte[_pathBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void GetBytes()

Assert.AreEqual((uint)_filenameBytes.Length, sshDataStream.ReadUInt32());
var actualFilename = new byte[_filenameBytes.Length];
sshDataStream.Read(actualFilename, 0, actualFilename.Length);
_ = sshDataStream.Read(actualFilename, 0, actualFilename.Length);
Assert.IsTrue(_filenameBytes.SequenceEqual(actualFilename));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public void GetBytes()

Assert.AreEqual((uint)_oldPathBytes.Length, sshDataStream.ReadUInt32());
var actualOldPath = new byte[_oldPathBytes.Length];
sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
_ = sshDataStream.Read(actualOldPath, 0, actualOldPath.Length);
Assert.IsTrue(_oldPathBytes.SequenceEqual(actualOldPath));

Assert.AreEqual((uint)_newPathBytes.Length, sshDataStream.ReadUInt32());
var actualNewPath = new byte[_newPathBytes.Length];
sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
_ = sshDataStream.Read(actualNewPath, 0, actualNewPath.Length);
Assert.IsTrue(_newPathBytes.SequenceEqual(actualNewPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public void GetBytes()

Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
var actualPath = new byte[_pathBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

var actualAttributes = new byte[_attributesBytes.Length];
sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
_ = sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
Assert.IsTrue(_attributesBytes.SequenceEqual(actualAttributes));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void GetBytes()

Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
var actualPath = new byte[_pathBytes.Length];
sshDataStream.Read(actualPath, 0, actualPath.Length);
_ = sshDataStream.Read(actualPath, 0, actualPath.Length);
Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public void GetBytes()

Assert.AreEqual((uint)_newLinkPathBytes.Length, sshDataStream.ReadUInt32());
var actualNewLinkPath = new byte[_newLinkPathBytes.Length];
sshDataStream.Read(actualNewLinkPath, 0, actualNewLinkPath.Length);
_ = sshDataStream.Read(actualNewLinkPath, 0, actualNewLinkPath.Length);
Assert.IsTrue(_newLinkPathBytes.SequenceEqual(actualNewLinkPath));

Assert.AreEqual((uint)_existingPathBytes.Length, sshDataStream.ReadUInt32());
var actualExistingPath = new byte[_existingPathBytes.Length];
sshDataStream.Read(actualExistingPath, 0, actualExistingPath.Length);
_ = sshDataStream.Read(actualExistingPath, 0, actualExistingPath.Length);
Assert.IsTrue(_existingPathBytes.SequenceEqual(actualExistingPath));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ public void GetBytes()

Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
var actualHandle = new byte[_handle.Length];
sshDataStream.Read(actualHandle, 0, actualHandle.Length);
_ = sshDataStream.Read(actualHandle, 0, actualHandle.Length);
Assert.IsTrue(_handle.SequenceEqual(actualHandle));

Assert.AreEqual(_serverFileOffset, sshDataStream.ReadUInt64());

Assert.AreEqual((uint)_length, sshDataStream.ReadUInt32());
var actualData = new byte[_length];
sshDataStream.Read(actualData, 0, actualData.Length);
_ = sshDataStream.Read(actualData, 0, actualData.Length);
Assert.IsTrue(_data.Take(_offset, _length).SequenceEqual(actualData));

Assert.IsTrue(sshDataStream.IsEndOfData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void ReadShouldThrowNotSupportedException()

try
{
_target.Read(buffer, 0, buffer.Length);
_ = _target.Read(buffer, 0, buffer.Length);
Assert.Fail();
}
catch (NotSupportedException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void ReadShouldThrowNotSupportedException()

try
{
_target.Read(buffer, 0, buffer.Length);
_ = _target.Read(buffer, 0, buffer.Length);
Assert.Fail();
}
catch (NotSupportedException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void ReadShouldThrowNotSupportedException()

try
{
_target.Read(buffer, 0, buffer.Length);
_ = _target.Read(buffer, 0, buffer.Length);
Assert.Fail();
}
catch (NotSupportedException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void ReadShouldThrowNotSupportedException()

try
{
_target.Read(buffer, 0, buffer.Length);
_ = _target.Read(buffer, 0, buffer.Length);
Assert.Fail();
}
catch (NotSupportedException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void ReadShouldThrowNotSupportedException()

try
{
_target.Read(buffer, 0, buffer.Length);
_ = _target.Read(buffer, 0, buffer.Length);
Assert.Fail();
}
catch (NotSupportedException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void ReadShouldThrowNotSupportedException()

try
{
_target.Read(buffer, 0, buffer.Length);
_ = _target.Read(buffer, 0, buffer.Length);
Assert.Fail();
}
catch (NotSupportedException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task ReadShouldThrowNotSupportedException()

try
{
await _target.ReadAsync(buffer, 0, buffer.Length);
_ = await _target.ReadAsync(buffer, 0, buffer.Length);
Assert.Fail();
}
catch (NotSupportedException ex)
Expand Down
Loading

0 comments on commit 784d484

Please sign in to comment.