From e2b16adec64d832e86c8a2c57479f4975b857250 Mon Sep 17 00:00:00 2001 From: Martin Date: Sat, 18 Dec 2021 04:16:00 +0100 Subject: [PATCH] Use pattern matching instead of as expression type checking --- src/Marr.Data/QGen/WhereBuilder.cs | 4 ++-- .../EnsureThat/ExpressionExtensions.cs | 3 +-- .../Instrumentation/Sentry/SentryTarget.cs | 4 +--- src/NzbDrone.Common/TinyIoC.cs | 12 +++--------- src/NzbDrone.Core.Test/Framework/MigrationTest.cs | 3 +-- .../Migration/147_swap_filechmod_for_folderchmod.cs | 5 ++--- src/NzbDrone.Core/Download/DownloadService.cs | 3 +-- src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs | 3 +-- .../Validation/EmptyCollectionValidator.cs | 4 +--- 9 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/Marr.Data/QGen/WhereBuilder.cs b/src/Marr.Data/QGen/WhereBuilder.cs index 64992d542..606c4621d 100644 --- a/src/Marr.Data/QGen/WhereBuilder.cs +++ b/src/Marr.Data/QGen/WhereBuilder.cs @@ -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; diff --git a/src/NzbDrone.Common/EnsureThat/ExpressionExtensions.cs b/src/NzbDrone.Common/EnsureThat/ExpressionExtensions.cs index dc4b25946..7e72fe4ed 100644 --- a/src/NzbDrone.Common/EnsureThat/ExpressionExtensions.cs +++ b/src/NzbDrone.Common/EnsureThat/ExpressionExtensions.cs @@ -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; diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs index f782b6dd1..cb2918ce4 100644 --- a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs @@ -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; diff --git a/src/NzbDrone.Common/TinyIoC.cs b/src/NzbDrone.Common/TinyIoC.cs index 7c5d7196f..7f5910a80 100644 --- a/src/NzbDrone.Common/TinyIoC.cs +++ b/src/NzbDrone.Common/TinyIoC.cs @@ -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(); } } diff --git a/src/NzbDrone.Core.Test/Framework/MigrationTest.cs b/src/NzbDrone.Core.Test/Framework/MigrationTest.cs index 4e538546e..35f9a5ab2 100644 --- a/src/NzbDrone.Core.Test/Framework/MigrationTest.cs +++ b/src/NzbDrone.Core.Test/Framework/MigrationTest.cs @@ -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); } diff --git a/src/NzbDrone.Core/Datastore/Migration/147_swap_filechmod_for_folderchmod.cs b/src/NzbDrone.Core/Datastore/Migration/147_swap_filechmod_for_folderchmod.cs index 3e0c53652..67b2c4366 100644 --- a/src/NzbDrone.Core/Datastore/Migration/147_swap_filechmod_for_folderchmod.cs +++ b/src/NzbDrone.Core/Datastore/Migration/147_swap_filechmod_for_folderchmod.cs @@ -21,9 +21,8 @@ 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()) { diff --git a/src/NzbDrone.Core/Download/DownloadService.cs b/src/NzbDrone.Core/Download/DownloadService.cs index 5f677f13e..8c9db6596 100644 --- a/src/NzbDrone.Core/Download/DownloadService.cs +++ b/src/NzbDrone.Core/Download/DownloadService.cs @@ -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); } diff --git a/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs b/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs index 5c02ae197..dc21c808e 100644 --- a/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs +++ b/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs @@ -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(); } } diff --git a/src/Sonarr.Http/Validation/EmptyCollectionValidator.cs b/src/Sonarr.Http/Validation/EmptyCollectionValidator.cs index b91604e79..287b103e3 100644 --- a/src/Sonarr.Http/Validation/EmptyCollectionValidator.cs +++ b/src/Sonarr.Http/Validation/EmptyCollectionValidator.cs @@ -15,9 +15,7 @@ namespace Sonarr.Http.Validation { if (context.PropertyValue == null) return true; - var collection = context.PropertyValue as IEnumerable; - - return collection != null && collection.Empty(); + return context.PropertyValue is IEnumerable collection && collection.Empty(); } } }