Skip to content

Commit 466cf37

Browse files
committed
Use 'is null' to test for null.
1 parent dd4718a commit 466cf37

35 files changed

+100
-104
lines changed

src/MySqlConnector.Logging.Microsoft.Extensions.Logging/MicrosoftExtensionsLoggingLoggerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private class MicrosoftExtensionsLoggingLogger : IMySqlConnectorLogger
1818

1919
public void Log(MySqlConnectorLogLevel level, string message, object[] args = null, Exception exception = null)
2020
{
21-
if (args == null || args.Length == 0)
21+
if (args is null || args.Length == 0)
2222
m_logger.Log(GetLevel(level), 0, message, exception, s_getMessage);
2323
else
2424
m_logger.Log(GetLevel(level), 0, (message, args), exception, s_messageFormatter);

src/MySqlConnector.Logging.Serilog/SerilogLoggerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public SerilogLogger(string name)
2626

2727
public void Log(MySqlConnectorLogLevel level, string message, object[] args = null, Exception exception = null)
2828
{
29-
if (args == null || args.Length == 0)
29+
if (args is null || args.Length == 0)
3030
m_logger.Write(GetLevel(level), exception, message);
3131
else
3232
{

src/MySqlConnector.Logging.log4net/Log4netLoggerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private class Log4netLogger : IMySqlConnectorLogger
2121

2222
public void Log(MySqlConnectorLogLevel level, string message, object[] args = null, Exception exception = null)
2323
{
24-
if (args == null || args.Length == 0)
24+
if (args is null || args.Length == 0)
2525
m_logger.Log(s_loggerType, GetLevel(level), message, exception);
2626
else
2727
m_logger.Log(s_loggerType, GetLevel(level), string.Format(CultureInfo.InvariantCulture, message, args), exception);

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public async Task ReapAsync(IOBehavior ioBehavior, CancellationToken cancellatio
214214
public Dictionary<string, CachedProcedure> GetProcedureCache()
215215
{
216216
var procedureCache = m_procedureCache;
217-
if (procedureCache == null)
217+
if (procedureCache is null)
218218
{
219219
var newProcedureCache = new Dictionary<string, CachedProcedure>();
220220
procedureCache = Interlocked.CompareExchange(ref m_procedureCache, newProcedureCache, null) ?? newProcedureCache;
@@ -290,7 +290,7 @@ private async Task CleanPoolAsync(IOBehavior ioBehavior, Func<ServerSession, boo
290290
m_sessions.RemoveLast();
291291
}
292292
}
293-
if (session == null)
293+
if (session is null)
294294
return;
295295

296296
if (shouldCleanFn(session))

src/MySqlConnector/Core/DbTypeMapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public object DoConversion(object obj)
1919
{
2020
if (obj.GetType() == ClrType)
2121
return obj;
22-
return m_convert == null ? Convert.ChangeType(obj, ClrType) : m_convert(obj);
22+
return m_convert is null ? Convert.ChangeType(obj, ClrType) : m_convert(obj);
2323
}
2424

2525
readonly Func<object, object> m_convert;

src/MySqlConnector/Core/NormalizedSchema.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ internal sealed class NormalizedSchema
1616
internal static NormalizedSchema MustNormalize(string name, string defaultSchema = null)
1717
{
1818
var normalized = new NormalizedSchema(name, defaultSchema);
19-
if (normalized.Component == null)
19+
if (normalized.Component is null)
2020
throw new ArgumentException("Could not determine function/procedure name", nameof(name));
21-
if (normalized.Schema == null)
21+
if (normalized.Schema is null)
2222
throw new ArgumentException("Could not determine schema", nameof(defaultSchema));
2323
return normalized;
2424
}
@@ -38,12 +38,12 @@ public NormalizedSchema(string name, string defaultSchema=null)
3838
firstGroup = match.Groups[1].Value.Trim();
3939
else if (match.Groups[2].Success)
4040
firstGroup = match.Groups[2].Value.Trim();
41-
if (Component == null)
41+
if (Component is null)
4242
Component = firstGroup.Trim();
4343
else
4444
Schema = firstGroup.Trim();
4545

46-
if (Schema == null)
46+
if (Schema is null)
4747
Schema = defaultSchema;
4848
}
4949
}

src/MySqlConnector/Core/PreparedStatementCommandExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private PayloadData CreateQueryPayload(PreparedStatement preparedStatement, MySq
8080
for (var i = 0; i < parameters.Length; i++)
8181
{
8282
var parameter = parameters[i];
83-
if (parameter.Value == null || parameter.Value == DBNull.Value)
83+
if (parameter.Value is null || parameter.Value == DBNull.Value)
8484
nullBitmap |= (byte) (1 << (i % 8));
8585

8686
if (i % 8 == 7)

src/MySqlConnector/Core/ResultSet.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public async Task<bool> ReadAsync(IOBehavior ioBehavior, CancellationToken cance
174174
? m_readBuffer.Dequeue()
175175
: await ScanRowAsync(ioBehavior, m_row, cancellationToken).ConfigureAwait(false);
176176

177-
if (m_row == null)
177+
if (m_row is null)
178178
{
179179
State = BufferState;
180180
return false;
@@ -187,7 +187,7 @@ public async Task<Row> BufferReadAsync(IOBehavior ioBehavior, CancellationToken
187187
{
188188
m_rowBuffered = m_rowBuffered?.Clone();
189189
// ScanRowAsync sets m_rowBuffered to the next row if there is one
190-
if (await ScanRowAsync(ioBehavior, null, cancellationToken).ConfigureAwait(false) == null)
190+
if (await ScanRowAsync(ioBehavior, null, cancellationToken).ConfigureAwait(false) is null)
191191
return null;
192192
m_readBuffer.Enqueue(m_rowBuffered);
193193
return m_rowBuffered;
@@ -245,7 +245,7 @@ Row ScanRowAsyncRemainder(PayloadData payload, Row row_)
245245
}
246246
}
247247

248-
if (row_ == null)
248+
if (row_ is null)
249249
row_ = DataReader.ResultSetProtocol == ResultSetProtocol.Binary ? (Row) new BinaryRow(this) : new TextRow(this);
250250
row_.SetData(payload.ArraySegment);
251251
m_rowBuffered = row_;
@@ -259,7 +259,7 @@ Row ScanRowAsyncRemainder(PayloadData payload, Row row_)
259259

260260
public string GetName(int ordinal)
261261
{
262-
if (ColumnDefinitions == null)
262+
if (ColumnDefinitions is null)
263263
throw new InvalidOperationException("There is no current result set.");
264264
if (ordinal < 0 || ordinal >= ColumnDefinitions.Length)
265265
throw new IndexOutOfRangeException("value must be between 0 and {0}".FormatInvariant(ColumnDefinitions.Length - 1));
@@ -268,7 +268,7 @@ public string GetName(int ordinal)
268268

269269
public string GetDataTypeName(int ordinal)
270270
{
271-
if (ColumnDefinitions == null)
271+
if (ColumnDefinitions is null)
272272
throw new InvalidOperationException("There is no current result set.");
273273
if (ordinal < 0 || ordinal >= ColumnDefinitions.Length)
274274
throw new IndexOutOfRangeException("value must be between 0 and {0}.".FormatInvariant(ColumnDefinitions.Length));
@@ -281,7 +281,7 @@ public string GetDataTypeName(int ordinal)
281281

282282
public Type GetFieldType(int ordinal)
283283
{
284-
if (ColumnDefinitions == null)
284+
if (ColumnDefinitions is null)
285285
throw new InvalidOperationException("There is no current result set.");
286286
if (ordinal < 0 || ordinal >= ColumnDefinitions.Length)
287287
throw new IndexOutOfRangeException("value must be between 0 and {0}.".FormatInvariant(ColumnDefinitions.Length));
@@ -306,9 +306,9 @@ public bool HasRows
306306

307307
public int GetOrdinal(string name)
308308
{
309-
if (name == null)
309+
if (name is null)
310310
throw new ArgumentNullException(nameof(name));
311-
if (ColumnDefinitions == null)
311+
if (ColumnDefinitions is null)
312312
throw new InvalidOperationException("There is no current result set.");
313313

314314
for (var column = 0; column < ColumnDefinitions.Length; column++)

src/MySqlConnector/Core/Row.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal abstract class Row
1313
public void SetData(ArraySegment<byte> data)
1414
{
1515
m_data = data;
16-
if (m_dataOffsets == null)
16+
if (m_dataOffsets is null)
1717
{
1818
m_dataOffsets = new int[ResultSet.ColumnDefinitions.Length];
1919
m_dataLengths = new int[ResultSet.ColumnDefinitions.Length];
@@ -76,7 +76,7 @@ public long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffs
7676
{
7777
CheckBinaryColumn(ordinal);
7878

79-
if (buffer == null)
79+
if (buffer is null)
8080
{
8181
// this isn't required by the DbDataReader.GetBytes API documentation, but is what mysql-connector-net does
8282
// (as does SqlDataReader: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getbytes.aspx)
@@ -100,7 +100,7 @@ public char GetChar(int ordinal)
100100
public long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
101101
{
102102
var value = GetString(ordinal);
103-
if (buffer == null)
103+
if (buffer is null)
104104
return value.Length;
105105

106106
CheckBufferArguments(dataOffset, buffer, bufferOffset, length);

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void AbortCancel(MySqlCommand command)
131131

132132
public void AddPreparedStatement(string commandText, PreparedStatements preparedStatements)
133133
{
134-
if (m_preparedStatements == null)
134+
if (m_preparedStatements is null)
135135
m_preparedStatements = new Dictionary<string, PreparedStatements>();
136136
m_preparedStatements.Add(commandText, preparedStatements);
137137
}
@@ -284,7 +284,7 @@ public async Task ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer
284284
throw new MySqlException((int) MySqlErrorCode.UnableToConnectToHost, null, "Unable to connect to any of the specified MySQL hosts.");
285285
}
286286

287-
var byteHandler = m_socket != null ? (IByteHandler) new SocketByteHandler(m_socket) : new StreamByteHandler(m_stream);
287+
var byteHandler = m_socket is null ? new StreamByteHandler(m_stream) : (IByteHandler) new SocketByteHandler(m_socket);
288288
m_payloadHandler = new StandardPayloadHandler(byteHandler);
289289

290290
payload = await ReceiveAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
@@ -352,7 +352,7 @@ public async Task ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer
352352
}
353353
} while (shouldRetrySsl);
354354

355-
if (m_supportsConnectionAttributes && cs.ConnectionAttributes == null)
355+
if (m_supportsConnectionAttributes && cs.ConnectionAttributes is null)
356356
cs.ConnectionAttributes = CreateConnectionAttributes(cs.ApplicationName);
357357

358358
using (var handshakeResponsePayload = HandshakeResponse41Payload.Create(initialHandshake, cs, m_useCompression, m_characterSet, m_supportsConnectionAttributes ? cs.ConnectionAttributes : null))
@@ -394,7 +394,7 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
394394
// clear all prepared statements; resetting the connection will clear them on the server
395395
ClearPreparedStatements();
396396

397-
if (DatabaseOverride == null && (ServerVersion.Version.CompareTo(ServerVersions.SupportsResetConnection) >= 0 || ServerVersion.MariaDbVersion?.CompareTo(ServerVersions.MariaDbSupportsResetConnection) >= 0))
397+
if (DatabaseOverride is null && (ServerVersion.Version.CompareTo(ServerVersions.SupportsResetConnection) >= 0 || ServerVersion.MariaDbVersion?.CompareTo(ServerVersions.MariaDbSupportsResetConnection) >= 0))
398398
{
399399
m_logArguments[1] = ServerVersion.OriginalString;
400400
Log.Debug("Session{0} ServerVersion={1} supports reset connection; sending reset connection request", m_logArguments);
@@ -410,7 +410,7 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
410410
else
411411
{
412412
// optimistically hash the password with the challenge from the initial handshake (supported by MariaDB; doesn't appear to be supported by MySQL)
413-
if (DatabaseOverride == null)
413+
if (DatabaseOverride is null)
414414
{
415415
m_logArguments[1] = ServerVersion.OriginalString;
416416
Log.Debug("Session{0} ServerVersion={1} doesn't support reset connection; sending change user request", m_logArguments);
@@ -918,7 +918,7 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
918918
var store = new X509Store(StoreName.My, storeLocation);
919919
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
920920

921-
if (cs.CertificateThumbprint == null)
921+
if (cs.CertificateThumbprint is null)
922922
{
923923
if (store.Certificates.Count == 0)
924924
{
@@ -1064,11 +1064,8 @@ bool ValidateRemoteCertificate(object rcbSender, X509Certificate rcbCertificate,
10641064
return rcbPolicyErrors == SslPolicyErrors.None;
10651065
}
10661066

1067-
SslStream sslStream;
1068-
if (clientCertificates == null)
1069-
sslStream = new SslStream(m_stream, false, ValidateRemoteCertificate);
1070-
else
1071-
sslStream = new SslStream(m_stream, false, ValidateRemoteCertificate, ValidateLocalCertificate);
1067+
var sslStream = clientCertificates is null ? new SslStream(m_stream, false, ValidateRemoteCertificate) :
1068+
new SslStream(m_stream, false, ValidateRemoteCertificate, ValidateLocalCertificate);
10721069

10731070
var checkCertificateRevocation = cs.SslMode == MySqlSslMode.VerifyFull;
10741071

src/MySqlConnector/Core/StoredProcedureCommandExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override async Task<DbDataReader> ExecuteReaderAsync(string commandText,
6262

6363
// if a return param is set, assume it is a funciton. otherwise, assume stored procedure
6464
commandText += "(" + string.Join(", ", argParamNames) +")";
65-
if (returnParam == null)
65+
if (returnParam is null)
6666
{
6767
commandText = inOutSetParams + "CALL " + commandText;
6868
if (m_outParams.Count > 0)

src/MySqlConnector/Logging/ConsoleLoggerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Log(MySqlConnectorLogLevel level, string message, object[] args = nu
3838
sb.Append(m_name);
3939
sb.Append('\t');
4040

41-
if (args == null || args.Length == 0)
41+
if (args is null || args.Length == 0)
4242
sb.Append(message);
4343
else
4444
sb.AppendFormat(CultureInfo.InvariantCulture, message, args);

src/MySqlConnector/MySql.Data.MySqlClient/MySqlBulkLoader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private string BuildSqlCommand()
105105
sqlCommandFragment.Clear();
106106
}
107107

108-
if (LinePrefix != null && LinePrefix.Length > 0)
108+
if (!string.IsNullOrEmpty(LinePrefix))
109109
sqlCommandFragment.AppendFormat("STARTING BY \'{0}\' ", LinePrefix);
110110

111111
if (LineTerminator != defaultLineTerminator)
@@ -145,7 +145,7 @@ private string BuildSqlCommand()
145145

146146
private async Task<int> LoadAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
147147
{
148-
if (Connection == null)
148+
if (Connection is null)
149149
throw new InvalidOperationException("Connection not set");
150150

151151
if (!string.IsNullOrWhiteSpace(FileName) && SourceStream != null)

src/MySqlConnector/MySql.Data.MySqlClient/MySqlCommand.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private MySqlCommand(MySqlCommand other)
6464
get
6565
{
6666
VerifyNotDisposed();
67-
if (m_parameterCollection == null)
67+
if (m_parameterCollection is null)
6868
m_parameterCollection = new MySqlParameterCollection();
6969
return m_parameterCollection;
7070
}
@@ -100,15 +100,15 @@ public override void Prepare()
100100
private Task PrepareAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
101101
{
102102
if (!NeedsPrepare(out var exception))
103-
return exception != null ? Utility.TaskFromException(exception) : Utility.CompletedTask;
103+
return exception is null ? Utility.CompletedTask : Utility.TaskFromException(exception);
104104

105105
return DoPrepareAsync(ioBehavior, cancellationToken);
106106
}
107107

108108
private bool NeedsPrepare(out Exception exception)
109109
{
110110
exception = null;
111-
if (Connection == null)
111+
if (Connection is null)
112112
exception = new InvalidOperationException("Connection property must be non-null.");
113113
else if (Connection.State != ConnectionState.Open)
114114
exception = new InvalidOperationException("Connection must be Open; current state is {0}".FormatInvariant(Connection.State));
@@ -127,7 +127,7 @@ private bool NeedsPrepare(out Exception exception)
127127
}
128128

129129
// don't prepare the same SQL twice
130-
return Connection.Session.TryGetPreparedStatement(CommandText) == null;
130+
return Connection.Session.TryGetPreparedStatement(CommandText) is null;
131131
}
132132

133133
private async Task DoPrepareAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
@@ -363,7 +363,7 @@ internal IDisposable RegisterCancel(CancellationToken token)
363363
if (!token.CanBeCanceled)
364364
return null;
365365

366-
if (m_cancelAction == null)
366+
if (m_cancelAction is null)
367367
m_cancelAction = Cancel;
368368
return token.Register(m_cancelAction);
369369
}
@@ -435,15 +435,15 @@ private bool IsValid(out Exception exception)
435435
exception = null;
436436
if (m_isDisposed)
437437
exception = new ObjectDisposedException(GetType().Name);
438-
else if (Connection == null)
438+
else if (Connection is null)
439439
exception = new InvalidOperationException("Connection property must be non-null.");
440440
else if (Connection.State != ConnectionState.Open && Connection.State != ConnectionState.Connecting)
441441
exception = new InvalidOperationException("Connection must be Open; current state is {0}".FormatInvariant(Connection.State));
442442
else if (!Connection.IgnoreCommandTransaction && Transaction != Connection.CurrentTransaction)
443443
exception = new InvalidOperationException("The transaction associated with this command is not the connection's active transaction; see https://fl.vu/mysql-trans");
444444
else if (string.IsNullOrWhiteSpace(CommandText))
445445
exception = new InvalidOperationException("CommandText must be specified");
446-
return exception == null;
446+
return exception is null;
447447
}
448448

449449
private PreparedStatements TryGetPreparedStatement() => CommandType == CommandType.Text && !string.IsNullOrWhiteSpace(CommandText) && m_connection != null &&

src/MySqlConnector/MySql.Data.MySqlClient/MySqlCommandBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class MySqlCommandBuilder
2121

2222
private static async Task DeriveParametersAsync(IOBehavior ioBehavior, MySqlCommand command, CancellationToken cancellationToken)
2323
{
24-
if (command == null)
24+
if (command is null)
2525
throw new ArgumentNullException(nameof(command));
2626
if (command.CommandType != CommandType.StoredProcedure)
2727
throw new ArgumentException("MySqlCommand.CommandType must be StoredProcedure not {0}".FormatInvariant(command.CommandType), nameof(command));
@@ -33,7 +33,7 @@ private static async Task DeriveParametersAsync(IOBehavior ioBehavior, MySqlComm
3333
throw new NotSupportedException("MySQL Server {0} doesn't support INFORMATION_SCHEMA".FormatInvariant(command.Connection.Session.ServerVersion.OriginalString));
3434

3535
var cachedProcedure = await command.Connection.GetCachedProcedure(ioBehavior, command.CommandText, cancellationToken).ConfigureAwait(false);
36-
if (cachedProcedure == null)
36+
if (cachedProcedure is null)
3737
{
3838
var name = NormalizedSchema.MustNormalize(command.CommandText, command.Connection.Database);
3939
throw new MySqlException("Procedure or function '{0}' cannot be found in database '{1}'.".FormatInvariant(name.Component, name.Schema));

0 commit comments

Comments
 (0)