Skip to content

Commit e4fb0c9

Browse files
committed
Use extension members for ThrowHelpers
1 parent 85c7895 commit e4fb0c9

File tree

64 files changed

+287
-333
lines changed

Some content is hidden

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

64 files changed

+287
-333
lines changed

src/Renci.SshNet/Abstractions/ThreadAbstraction.cs

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

5-
using Renci.SshNet.Common;
6-
75
namespace Renci.SshNet.Abstractions
86
{
97
internal static class ThreadAbstraction
@@ -18,7 +16,7 @@ internal static class ThreadAbstraction
1816
/// </returns>
1917
public static Task ExecuteThreadLongRunning(Action action)
2018
{
21-
ThrowHelper.ThrowIfNull(action);
19+
ArgumentNullException.ThrowIfNull(action);
2220

2321
return Task.Factory.StartNew(action,
2422
CancellationToken.None,
@@ -32,7 +30,7 @@ public static Task ExecuteThreadLongRunning(Action action)
3230
/// <param name="action">The action to execute.</param>
3331
public static void ExecuteThread(Action action)
3432
{
35-
ThrowHelper.ThrowIfNull(action);
33+
ArgumentNullException.ThrowIfNull(action);
3634

3735
_ = ThreadPool.QueueUserWorkItem(o => action());
3836
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#nullable enable
2+
#if !NET
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Runtime.CompilerServices;
5+
6+
namespace System
7+
{
8+
internal static class ThrowExtensions
9+
{
10+
extension(ObjectDisposedException)
11+
{
12+
public static void ThrowIf(bool condition, object instance)
13+
{
14+
if (condition)
15+
{
16+
Throw(instance);
17+
18+
static void Throw(object? instance)
19+
{
20+
throw new ObjectDisposedException(instance?.GetType().FullName);
21+
}
22+
}
23+
}
24+
}
25+
26+
extension(ArgumentNullException)
27+
{
28+
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
29+
{
30+
if (argument is null)
31+
{
32+
ThrowArgumentNullException(paramName);
33+
}
34+
35+
[DoesNotReturn]
36+
static void ThrowArgumentNullException(string? paramName)
37+
{
38+
throw new ArgumentNullException(paramName);
39+
}
40+
}
41+
}
42+
43+
extension(ArgumentException)
44+
{
45+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
46+
{
47+
if (string.IsNullOrWhiteSpace(argument))
48+
{
49+
Throw(argument, paramName);
50+
51+
[DoesNotReturn]
52+
static void Throw(string? argument, string? paramName)
53+
{
54+
ThrowIfNull(argument, paramName);
55+
throw new ArgumentException("The value cannot be an empty string or composed entirely of whitespace.", paramName);
56+
}
57+
}
58+
}
59+
60+
public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
61+
{
62+
if (string.IsNullOrEmpty(argument))
63+
{
64+
Throw(argument, paramName);
65+
66+
[DoesNotReturn]
67+
static void Throw(string? argument, string? paramName)
68+
{
69+
ThrowIfNull(argument, paramName);
70+
throw new ArgumentException("The value cannot be an empty string.", paramName);
71+
}
72+
}
73+
}
74+
}
75+
76+
extension(ArgumentOutOfRangeException)
77+
{
78+
public static void ThrowIfNegative(long value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
79+
{
80+
if (value < 0)
81+
{
82+
Throw(value, paramName);
83+
84+
[DoesNotReturn]
85+
static void Throw(long value, string? paramName)
86+
{
87+
throw new ArgumentOutOfRangeException(paramName, value, "Value must be non-negative.");
88+
}
89+
}
90+
}
91+
}
92+
}
93+
}
94+
#endif

src/Renci.SshNet/AuthenticationMethod.cs

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

3-
using Renci.SshNet.Common;
4-
53
namespace Renci.SshNet
64
{
75
/// <summary>
@@ -36,7 +34,7 @@ public abstract class AuthenticationMethod : IAuthenticationMethod, IDisposable
3634
/// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</exception>
3735
protected AuthenticationMethod(string username)
3836
{
39-
ThrowHelper.ThrowIfNullOrWhiteSpace(username);
37+
ArgumentException.ThrowIfNullOrWhiteSpace(username);
4038

4139
Username = username;
4240
}

src/Renci.SshNet/BaseClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
186186
/// </remarks>
187187
private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
188188
{
189-
ThrowHelper.ThrowIfNull(connectionInfo);
190-
ThrowHelper.ThrowIfNull(serviceFactory);
189+
ArgumentNullException.ThrowIfNull(connectionInfo);
190+
ArgumentNullException.ThrowIfNull(serviceFactory);
191191

192192
_connectionInfo = connectionInfo;
193193
_ownsConnectionInfo = ownsConnectionInfo;
@@ -467,7 +467,7 @@ protected virtual void Dispose(bool disposing)
467467
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
468468
protected void CheckDisposed()
469469
{
470-
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
470+
ObjectDisposedException.ThrowIf(_isDisposed, this);
471471
}
472472

473473
/// <summary>

src/Renci.SshNet/ClientAuthentication.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +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-
ThrowHelper.ThrowIfNull(connectionInfo);
56-
ThrowHelper.ThrowIfNull(session);
55+
ArgumentNullException.ThrowIfNull(connectionInfo);
56+
ArgumentNullException.ThrowIfNull(session);
5757

5858
session.RegisterMessage("SSH_MSG_USERAUTH_FAILURE");
5959
session.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS");

src/Renci.SshNet/Common/ChannelDataEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class ChannelDataEventArgs : ChannelEventArgs
1616
public ChannelDataEventArgs(uint channelNumber, ArraySegment<byte> data)
1717
: base(channelNumber)
1818
{
19-
ThrowHelper.ThrowIfNull(data.Array);
19+
ArgumentNullException.ThrowIfNull(data.Array);
2020

2121
Data = data;
2222
}

src/Renci.SshNet/Common/ChannelInputStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public override void Write(byte[] buffer, int offset, int count)
106106
#endif
107107
ValidateBufferArguments(buffer, offset, count);
108108

109-
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
109+
ObjectDisposedException.ThrowIf(_isDisposed, this);
110110

111111
if (count == 0)
112112
{

src/Renci.SshNet/Common/ChannelRequestEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal sealed class ChannelRequestEventArgs : EventArgs
1616
/// <exception cref="ArgumentNullException"><paramref name="info"/> is <see langword="null"/>.</exception>
1717
public ChannelRequestEventArgs(RequestInfo info)
1818
{
19-
ThrowHelper.ThrowIfNull(info);
19+
ArgumentNullException.ThrowIfNull(info);
2020

2121
Info = info;
2222
}

src/Renci.SshNet/Common/Extensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ internal static void ValidatePort(this int value, [CallerArgumentExpression(name
193193
/// </remarks>
194194
public static byte[] Take(this byte[] value, int offset, int count)
195195
{
196-
ThrowHelper.ThrowIfNull(value);
196+
ArgumentNullException.ThrowIfNull(value);
197197

198198
if (count == 0)
199199
{
@@ -225,7 +225,7 @@ public static byte[] Take(this byte[] value, int offset, int count)
225225
/// </remarks>
226226
public static byte[] Take(this byte[] value, int count)
227227
{
228-
ThrowHelper.ThrowIfNull(value);
228+
ArgumentNullException.ThrowIfNull(value);
229229

230230
if (count == 0)
231231
{
@@ -244,8 +244,8 @@ public static byte[] Take(this byte[] value, int count)
244244

245245
public static bool IsEqualTo(this byte[] left, byte[] right)
246246
{
247-
ThrowHelper.ThrowIfNull(left);
248-
ThrowHelper.ThrowIfNull(right);
247+
ArgumentNullException.ThrowIfNull(left);
248+
ArgumentNullException.ThrowIfNull(right);
249249

250250
return left.AsSpan().SequenceEqual(right);
251251
}
@@ -259,7 +259,7 @@ public static bool IsEqualTo(this byte[] left, byte[] right)
259259
/// </returns>
260260
public static byte[] TrimLeadingZeros(this byte[] value)
261261
{
262-
ThrowHelper.ThrowIfNull(value);
262+
ArgumentNullException.ThrowIfNull(value);
263263

264264
for (var i = 0; i < value.Length; i++)
265265
{

src/Renci.SshNet/Common/HostKeyEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public string FingerPrintMD5
9797
/// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception>
9898
public HostKeyEventArgs(KeyHostAlgorithm host)
9999
{
100-
ThrowHelper.ThrowIfNull(host);
100+
ArgumentNullException.ThrowIfNull(host);
101101

102102
CanTrust = true;
103103
HostKey = host.KeyData.GetBytes();

0 commit comments

Comments
 (0)