Skip to content

Commit aac10fb

Browse files
authored
Miscellaneous cleanup/throw helpers (#1491)
* PrivateKeyFile stuff * Extract from net7 branch * more ThrowHelper stuff
1 parent 2ce54c0 commit aac10fb

File tree

94 files changed

+521
-1443
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+521
-1443
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
Disable nullable warnings on old frameworks because of missing annotations.
3333
-->
3434
<PropertyGroup Condition=" !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0')) ">
35-
<NoWarn>$(NoWarn);CS8602</NoWarn>
35+
<NoWarn>$(NoWarn);CS8602;CS8604;CS8777</NoWarn>
3636
</PropertyGroup>
3737

3838
<!--

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PackageVersion Include="MSTest.TestFramework" Version="3.6.0" />
1919
<PackageVersion Include="Moq" Version="4.20.72" />
2020
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.7.70-alpha" />
21+
<PackageVersion Include="PolySharp" Version="1.14.1" />
2122
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.19.0.84025" />
2223
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
2324
<PackageVersion Include="System.Formats.Asn1" Version="8.0.1" />

src/Renci.SshNet/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,6 @@ dotnet_diagnostic.MA0040.severity = none
188188
# MA0042: Do not use blocking calls in an async method
189189
# duplicate of CA1849
190190
dotnet_diagnostic.MA0042.severity = none
191+
192+
# S3236: Caller information arguments should not be provided explicitly
193+
dotnet_diagnostic.S3236.severity = none

src/Renci.SshNet/Abstractions/SocketAbstraction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static void ClearReadBuffer(Socket socket)
129129

130130
public static int ReadPartial(Socket socket, byte[] buffer, int offset, int size, TimeSpan timeout)
131131
{
132-
socket.ReceiveTimeout = timeout.AsTimeout(nameof(timeout));
132+
socket.ReceiveTimeout = timeout.AsTimeout();
133133

134134
try
135135
{
@@ -274,7 +274,7 @@ public static int Read(Socket socket, byte[] buffer, int offset, int size, TimeS
274274
var totalBytesRead = 0;
275275
var totalBytesToRead = size;
276276

277-
socket.ReceiveTimeout = readTimeout.AsTimeout(nameof(readTimeout));
277+
socket.ReceiveTimeout = readTimeout.AsTimeout();
278278

279279
do
280280
{

src/Renci.SshNet/Abstractions/ThreadAbstraction.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44

5+
using Renci.SshNet.Common;
6+
57
namespace Renci.SshNet.Abstractions
68
{
79
internal static class ThreadAbstraction
@@ -16,10 +18,7 @@ internal static class ThreadAbstraction
1618
/// </returns>
1719
public static Task ExecuteThreadLongRunning(Action action)
1820
{
19-
if (action is null)
20-
{
21-
throw new ArgumentNullException(nameof(action));
22-
}
21+
ThrowHelper.ThrowIfNull(action);
2322

2423
return Task.Factory.StartNew(action,
2524
CancellationToken.None,
@@ -33,10 +32,7 @@ public static Task ExecuteThreadLongRunning(Action action)
3332
/// <param name="action">The action to execute.</param>
3433
public static void ExecuteThread(Action action)
3534
{
36-
if (action is null)
37-
{
38-
throw new ArgumentNullException(nameof(action));
39-
}
35+
ThrowHelper.ThrowIfNull(action);
4036

4137
_ = ThreadPool.QueueUserWorkItem(o => action());
4238
}

src/Renci.SshNet/AuthenticationMethod.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using Renci.SshNet.Common;
4+
35
namespace Renci.SshNet
46
{
57
/// <summary>
@@ -34,10 +36,7 @@ public abstract class AuthenticationMethod : IAuthenticationMethod
3436
/// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</exception>
3537
protected AuthenticationMethod(string username)
3638
{
37-
if (string.IsNullOrWhiteSpace(username))
38-
{
39-
throw new ArgumentException("username");
40-
}
39+
ThrowHelper.ThrowIfNullOrWhiteSpace(username);
4140

4241
Username = username;
4342
}

src/Renci.SshNet/BaseClient.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,8 @@ protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
184184
/// </remarks>
185185
private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
186186
{
187-
if (connectionInfo is null)
188-
{
189-
throw new ArgumentNullException(nameof(connectionInfo));
190-
}
191-
192-
if (serviceFactory is null)
193-
{
194-
throw new ArgumentNullException(nameof(serviceFactory));
195-
}
187+
ThrowHelper.ThrowIfNull(connectionInfo);
188+
ThrowHelper.ThrowIfNull(serviceFactory);
196189

197190
_connectionInfo = connectionInfo;
198191
_ownsConnectionInfo = ownsConnectionInfo;
@@ -458,17 +451,10 @@ protected virtual void Dispose(bool disposing)
458451
/// <summary>
459452
/// Check if the current instance is disposed.
460453
/// </summary>
461-
/// <exception cref="ObjectDisposedException">THe current instance is disposed.</exception>
454+
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
462455
protected void CheckDisposed()
463456
{
464-
#if NET7_0_OR_GREATER
465-
ObjectDisposedException.ThrowIf(_isDisposed, this);
466-
#else
467-
if (_isDisposed)
468-
{
469-
throw new ObjectDisposedException(GetType().FullName);
470-
}
471-
#endif // NET7_0_OR_GREATER
457+
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
472458
}
473459

474460
/// <summary>

src/Renci.SshNet/ClientAuthentication.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,8 @@ internal int PartialSuccessLimit
5252
/// <exception cref="SshAuthenticationException">Failed to authenticate the client.</exception>
5353
public void Authenticate(IConnectionInfoInternal connectionInfo, ISession session)
5454
{
55-
if (connectionInfo is null)
56-
{
57-
throw new ArgumentNullException(nameof(connectionInfo));
58-
}
59-
60-
if (session is null)
61-
{
62-
throw new ArgumentNullException(nameof(session));
63-
}
55+
ThrowHelper.ThrowIfNull(connectionInfo);
56+
ThrowHelper.ThrowIfNull(session);
6457

6558
session.RegisterMessage("SSH_MSG_USERAUTH_FAILURE");
6659
session.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS");

src/Renci.SshNet/Common/ChannelDataEventArgs.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ internal class ChannelDataEventArgs : ChannelEventArgs
1616
public ChannelDataEventArgs(uint channelNumber, byte[] data)
1717
: base(channelNumber)
1818
{
19-
if (data is null)
20-
{
21-
throw new ArgumentNullException(nameof(data));
22-
}
19+
ThrowHelper.ThrowIfNull(data);
2320

2421
Data = data;
2522
}

src/Renci.SshNet/Common/ChannelInputStream.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ public override int Read(byte[] buffer, int offset, int count)
101101
/// <exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
102102
public override void Write(byte[] buffer, int offset, int count)
103103
{
104-
if (buffer == null)
105-
{
106-
throw new ArgumentNullException(nameof(buffer));
107-
}
104+
ThrowHelper.ThrowIfNull(buffer);
108105

109106
if (offset + count > buffer.Length)
110107
{
@@ -116,10 +113,7 @@ public override void Write(byte[] buffer, int offset, int count)
116113
throw new ArgumentOutOfRangeException(nameof(offset), "offset or count is negative.");
117114
}
118115

119-
if (_isDisposed)
120-
{
121-
throw CreateObjectDisposedException();
122-
}
116+
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
123117

124118
if (count == 0)
125119
{
@@ -208,10 +202,5 @@ public override long Position
208202
get { return _totalPosition; }
209203
set { throw new NotSupportedException(); }
210204
}
211-
212-
private ObjectDisposedException CreateObjectDisposedException()
213-
{
214-
return new ObjectDisposedException(GetType().FullName);
215-
}
216205
}
217206
}

0 commit comments

Comments
 (0)