Skip to content

Commit 9dd55dd

Browse files
committed
Use throw expressions.
1 parent caffe39 commit 9dd55dd

File tree

10 files changed

+18
-66
lines changed

10 files changed

+18
-66
lines changed

src/MySqlConnector/ByteArrayReader.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@ internal sealed class ByteArrayReader
66
{
77
public ByteArrayReader(byte[] buffer, int offset, int length)
88
{
9-
if (buffer == null)
10-
throw new ArgumentNullException(nameof(buffer));
11-
if (offset < 0)
12-
throw new ArgumentOutOfRangeException(nameof(offset));
13-
if (offset + length > buffer.Length)
14-
throw new ArgumentOutOfRangeException(nameof(length));
15-
16-
m_buffer = buffer;
17-
m_maxOffset = offset + length;
18-
m_offset = offset;
9+
m_buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
10+
m_offset = offset >= 0 ? offset : throw new ArgumentOutOfRangeException(nameof(offset));
11+
m_maxOffset = offset + length <= m_buffer.Length ? offset + length : throw new ArgumentOutOfRangeException(nameof(length));
1912
}
2013

2114
public ByteArrayReader(ArraySegment<byte> arraySegment)
@@ -26,12 +19,7 @@ public ByteArrayReader(ArraySegment<byte> arraySegment)
2619
public int Offset
2720
{
2821
get { return m_offset; }
29-
set
30-
{
31-
if (value < 0 || value > m_maxOffset)
32-
throw new ArgumentOutOfRangeException(nameof(value), "value must be between 0 and {0}".FormatInvariant(m_maxOffset));
33-
m_offset = value;
34-
}
22+
set { m_offset = value >= 0 && value <= m_maxOffset ? value : throw new ArgumentOutOfRangeException(nameof(value), "value must be between 0 and {0}".FormatInvariant(m_maxOffset)); }
3523
}
3624

3725
public byte ReadByte()

src/MySqlConnector/MySqlClient/Caches/CachedProcedure.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,12 @@ internal MySqlParameterCollection AlignParamsWithDb(MySqlParameterCollection par
6767
MySqlParameter alignParam;
6868
if (cachedParam.Direction == ParameterDirection.ReturnValue)
6969
{
70-
if (returnParam == null)
71-
throw new InvalidOperationException($"Attempt to call stored function {FullyQualified} without specifying a return parameter");
72-
alignParam = returnParam;
70+
alignParam = returnParam ?? throw new InvalidOperationException($"Attempt to call stored function {FullyQualified} without specifying a return parameter");
7371
}
7472
else
7573
{
7674
var index = parameterCollection.NormalizedIndexOf(cachedParam.Name);
77-
if (index == -1)
78-
throw new ArgumentException($"Parameter '{cachedParam.Name}' not found in the collection.");
79-
alignParam = parameterCollection[index];
75+
alignParam = index >= 0 ? parameterCollection[index] : throw new ArgumentException($"Parameter '{cachedParam.Name}' not found in the collection.");
8076
}
8177

8278
if (alignParam.Direction == default(ParameterDirection))

src/MySqlConnector/MySqlClient/MySqlConnectionStringBuilder.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,8 @@ internal abstract class MySqlConnectionStringOption
244244
public static MySqlConnectionStringOption TryGetOptionForKey(string key) =>
245245
s_options.TryGetValue(key, out var option) ? option : null;
246246

247-
public static MySqlConnectionStringOption GetOptionForKey(string key)
248-
{
249-
var option = TryGetOptionForKey(key);
250-
if (option == null)
251-
throw new InvalidOperationException("Option '{0}' not supported.".FormatInvariant(key));
252-
return option;
253-
}
247+
public static MySqlConnectionStringOption GetOptionForKey(string key) =>
248+
TryGetOptionForKey(key) ?? throw new InvalidOperationException("Option '{0}' not supported.".FormatInvariant(key));
254249

255250
public string Key => m_keys[0];
256251
public IReadOnlyList<string> Keys => m_keys;

src/MySqlConnector/MySqlClient/MySqlDataReader.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,9 @@ public override long GetChars(int ordinal, long dataOffset, char[] buffer, int b
160160

161161
public override IEnumerator GetEnumerator() => new DbEnumerator(this, closeReader: false);
162162

163-
public override int Depth
164-
{
165-
get { throw new NotSupportedException(); }
166-
}
163+
public override int Depth => throw new NotSupportedException();
167164

168-
protected override DbDataReader GetDbDataReader(int ordinal)
169-
{
170-
throw new NotSupportedException();
171-
}
165+
protected override DbDataReader GetDbDataReader(int ordinal) => throw new NotSupportedException();
172166

173167
public override DateTime GetDateTime(int ordinal) => GetResultSet().GetCurrentRow().GetDateTime(ordinal);
174168

@@ -185,10 +179,7 @@ protected override DbDataReader GetDbDataReader(int ordinal)
185179
public override int VisibleFieldCount => FieldCount;
186180

187181
#if !NETSTANDARD1_3
188-
public override DataTable GetSchemaTable()
189-
{
190-
throw new NotSupportedException();
191-
}
182+
public override DataTable GetSchemaTable() => throw new NotSupportedException();
192183

193184
public override void Close()
194185
{
@@ -280,9 +271,7 @@ private void VerifyNotDisposed()
280271
private ResultSet GetResultSet()
281272
{
282273
VerifyNotDisposed();
283-
if (m_resultSet == null)
284-
throw new InvalidOperationException("There is no current result set.");
285-
return m_resultSet;
274+
return m_resultSet ?? throw new InvalidOperationException("There is no current result set.");
286275
}
287276

288277
readonly CommandBehavior m_behavior;

src/MySqlConnector/MySqlClient/MySqlParser.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ internal abstract class MySqlParser
66
{
77
public int Parse(string sql)
88
{
9-
if (sql == null)
10-
throw new ArgumentNullException(nameof(sql));
11-
12-
OnBeforeParse(sql);
9+
OnBeforeParse(sql ?? throw new ArgumentNullException(nameof(sql)));
1310

1411
int statementCount = 0;
1512
int parameterStartIndex = -1;

src/MySqlConnector/MySqlClient/Results/ResultSet.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,7 @@ public Row GetCurrentRow()
384384
{
385385
if (State != ResultSetState.ReadingRows)
386386
throw new InvalidOperationException("Read must be called first.");
387-
if (m_row == null)
388-
throw new InvalidOperationException("There is no current row.");
389-
return m_row;
387+
return m_row ?? throw new InvalidOperationException("There is no current row.");
390388
}
391389

392390
public readonly MySqlDataReader DataReader;

src/MySqlConnector/MySqlClient/Results/Row.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ public char GetChar(int ordinal)
128128
return (char) GetValue(ordinal);
129129
}
130130

131-
public long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
132-
{
133-
throw new NotImplementedException();
134-
}
131+
public long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length) => throw new NotImplementedException();
135132

136133
public Guid GetGuid(int ordinal)
137134
{

src/MySqlConnector/Protocol/Serialization/CompressedPayloadHandler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,7 @@ public CompressedByteHandler(CompressedPayloadHandler compressedPayloadHandler,
243243
public ValueTask<int> ReadBytesAsync(ArraySegment<byte> buffer, IOBehavior ioBehavior) =>
244244
m_compressedPayloadHandler.ReadBytesAsync(buffer, m_protocolErrorBehavior, ioBehavior);
245245

246-
public ValueTask<int> WriteBytesAsync(ArraySegment<byte> data, IOBehavior ioBehavior)
247-
{
248-
throw new NotSupportedException();
249-
}
246+
public ValueTask<int> WriteBytesAsync(ArraySegment<byte> data, IOBehavior ioBehavior) => throw new NotSupportedException();
250247

251248
readonly CompressedPayloadHandler m_compressedPayloadHandler;
252249
readonly ProtocolErrorBehavior m_protocolErrorBehavior;

src/MySqlConnector/Protocol/Serialization/StandardPayloadHandler.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public IByteHandler ByteHandler
2020
get { return m_byteHandler; }
2121
set
2222
{
23-
if (value == null)
24-
throw new ArgumentNullException(nameof(value));
25-
m_byteHandler = value;
23+
m_byteHandler = value ?? throw new ArgumentNullException(nameof(value));
2624
m_bufferedByteReader = new BufferedByteReader();
2725
}
2826
}

src/MySqlConnector/SocketAwaitable.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ internal sealed class SocketAwaitable : INotifyCompletion
1111
{
1212
public SocketAwaitable(SocketAsyncEventArgs eventArgs)
1313
{
14-
if (eventArgs == null)
15-
throw new ArgumentNullException(nameof(eventArgs));
16-
17-
EventArgs = eventArgs;
14+
EventArgs = eventArgs ?? throw new ArgumentNullException(nameof(eventArgs));
1815
eventArgs.Completed += (s, e) => (m_continuation ?? Interlocked.CompareExchange(ref m_continuation, s_sentinel, null))?.Invoke();
1916
}
2017

0 commit comments

Comments
 (0)