Use pattern matching instead of as expression type checking

This commit is contained in:
Martin 2021-12-18 04:16:00 +01:00 committed by GitHub
parent 201004113e
commit e2b16adec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 13 additions and 28 deletions

View File

@ -148,8 +148,8 @@ namespace Marr.Data.QGen
if (right == null) // Value is not directly passed in as a constant
{
var rightMemberExp = (rightExpression as MemberExpression);
var parentMemberExpression = rightMemberExp.Expression as MemberExpression;
if (parentMemberExpression != null) // Value is passed in as a property on a parent entity
if (rightMemberExp.Expression is MemberExpression parentMemberExpression) // Value is passed in as a property on a parent entity
{
string entityName = (rightMemberExp.Expression as MemberExpression).Member.Name;
var container = ((rightMemberExp.Expression as MemberExpression).Expression as ConstantExpression).Value;

View File

@ -7,9 +7,8 @@ namespace NzbDrone.Common.EnsureThat
internal static string ToPath(this MemberExpression e)
{
var path = "";
var parent = e.Expression as MemberExpression;
if (parent != null)
if (e.Expression is MemberExpression parent)
path = parent.ToPath() + ".";
return path + e.Member.Name;

View File

@ -160,9 +160,7 @@ namespace NzbDrone.Common.Instrumentation.Sentry
private void OnError(Exception ex)
{
var webException = ex as WebException;
if (webException != null)
if (ex is WebException webException)
{
var response = webException.Response as HttpWebResponse;
var statusCode = response?.StatusCode;

View File

@ -2629,9 +2629,7 @@ namespace TinyIoC
public void Dispose()
{
var disposable = _instance as IDisposable;
if (disposable != null)
if (_instance is IDisposable disposable)
disposable.Dispose();
}
}
@ -2693,9 +2691,7 @@ namespace TinyIoC
public void Dispose()
{
var disposable = _instance.Target as IDisposable;
if (disposable != null)
if (_instance.Target is IDisposable disposable)
disposable.Dispose();
}
}
@ -2763,9 +2759,7 @@ namespace TinyIoC
if (this._Current == null)
return;
var disposable = this._Current as IDisposable;
if (disposable != null)
if (this._Current is IDisposable disposable)
disposable.Dispose();
}
}

View File

@ -25,8 +25,7 @@ namespace NzbDrone.Core.Test.Framework
{
BeforeMigration = m =>
{
var migration = m as TMigration;
if (beforeMigration != null && migration != null)
if (beforeMigration != null && m is TMigration migration)
{
beforeMigration(migration);
}

View File

@ -22,8 +22,7 @@ namespace NzbDrone.Core.Datastore.Migration
getFileChmodCmd.Transaction = tran;
getFileChmodCmd.CommandText = @"SELECT Value FROM Config WHERE Key = 'filechmod'";
var fileChmod = getFileChmodCmd.ExecuteScalar() as string;
if (fileChmod != null)
if (getFileChmodCmd.ExecuteScalar() is string fileChmod)
{
if (fileChmod.IsNotNullOrWhiteSpace())
{

View File

@ -88,8 +88,7 @@ namespace NzbDrone.Core.Download
}
catch (ReleaseDownloadException ex)
{
var http429 = ex.InnerException as TooManyRequestsException;
if (http429 != null)
if (ex.InnerException is TooManyRequestsException http429)
{
_indexerStatusService.RecordFailure(remoteEpisode.Release.IndexerId, http429.RetryAfter);
}

View File

@ -123,8 +123,7 @@ namespace NzbDrone.Test.Common.AutoMoq
{
foreach (var registeredMock in _registeredMocks)
{
var mock = registeredMock.Value as Mock;
if (mock != null)
if (registeredMock.Value is Mock mock)
mock.VerifyAll();
}
}

View File

@ -15,9 +15,7 @@ namespace Sonarr.Http.Validation
{
if (context.PropertyValue == null) return true;
var collection = context.PropertyValue as IEnumerable<T>;
return collection != null && collection.Empty();
return context.PropertyValue is IEnumerable<T> collection && collection.Empty();
}
}
}