diff --git a/src/Files.App/Extensions/EnumExtensions.cs b/src/Files.App/Extensions/EnumExtensions.cs index e1feaff30c2b..3d5a4b7ecbde 100644 --- a/src/Files.App/Extensions/EnumExtensions.cs +++ b/src/Files.App/Extensions/EnumExtensions.cs @@ -15,7 +15,7 @@ public static TEnum GetEnum(string text) where TEnum : struct throw new InvalidOperationException("Generic parameter 'TEnum' must be an enum."); } - return (TEnum)Enum.Parse(typeof(TEnum), text); + return Enum.Parse(text); } } } diff --git a/src/Files.App/Helpers/WMI/ManagementEventWatcher.cs b/src/Files.App/Helpers/WMI/ManagementEventWatcher.cs index 6de09f630c47..5c74ae665683 100644 --- a/src/Files.App/Helpers/WMI/ManagementEventWatcher.cs +++ b/src/Files.App/Helpers/WMI/ManagementEventWatcher.cs @@ -51,10 +51,7 @@ public ManagementEventWatcher(WqlEventQuery query) { string queryExpression = query.QueryExpression; - if (string.IsNullOrWhiteSpace(queryExpression)) - { - throw new ArgumentNullException(nameof(queryExpression)); - } + ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression); _nameSpace = DefaultNameSpace; _queryDialect = DefaultQueryDialect; @@ -70,10 +67,7 @@ public ManagementEventWatcher(WqlEventQuery query) /// public ManagementEventWatcher(string queryDialect, string queryExpression) { - if (string.IsNullOrWhiteSpace(queryExpression)) - { - throw new ArgumentNullException(nameof(queryExpression)); - } + ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression); _nameSpace = DefaultNameSpace; _queryDialect = queryDialect ?? DefaultQueryDialect; @@ -90,10 +84,7 @@ public ManagementEventWatcher(string queryDialect, string queryExpression) /// public ManagementEventWatcher(string nameSpace, string queryDialect, string queryExpression) { - if (string.IsNullOrWhiteSpace(queryExpression)) - { - throw new ArgumentNullException(nameof(queryExpression)); - } + ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression); _nameSpace = nameSpace ?? DefaultNameSpace; _queryDialect = queryDialect ?? DefaultQueryDialect; @@ -111,10 +102,7 @@ public ManagementEventWatcher(string nameSpace, string queryDialect, string quer /// public ManagementEventWatcher(string computerName, string nameSpace, string queryDialect, string queryExpression) { - if (string.IsNullOrWhiteSpace(queryExpression)) - { - throw new ArgumentNullException(nameof(queryExpression)); - } + ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression); _computerName = computerName; _nameSpace = nameSpace ?? DefaultNameSpace; @@ -160,10 +148,7 @@ public void Start() { lock (_myLock) { - if (_isDisposed) - { - throw new ObjectDisposedException(nameof(ManagementEventWatcher)); - } + ObjectDisposedException.ThrowIf(_isDisposed, this); if (_cimWatcherStatus != CimWatcherStatus.Default && _cimWatcherStatus != CimWatcherStatus.Stopped) { @@ -180,10 +165,7 @@ public void Stop() { lock (_myLock) { - if (_isDisposed) - { - throw new ObjectDisposedException(nameof(ManagementEventWatcher)); - } + ObjectDisposedException.ThrowIf(_isDisposed, this); if (_cimWatcherStatus != CimWatcherStatus.Started) { diff --git a/src/Files.App/Services/App/AppUpdateSideloadService.cs b/src/Files.App/Services/App/AppUpdateSideloadService.cs index 25df4e538652..bbea36a8509e 100644 --- a/src/Files.App/Services/App/AppUpdateSideloadService.cs +++ b/src/Files.App/Services/App/AppUpdateSideloadService.cs @@ -87,8 +87,7 @@ public async Task CheckForUpdatesAsync() XmlSerializer xml = new XmlSerializer(typeof(AppInstaller)); var appInstaller = (AppInstaller?)xml.Deserialize(stream); - if (appInstaller is null) - throw new ArgumentNullException(nameof(appInstaller)); + ArgumentNullException.ThrowIfNull(appInstaller); var remoteVersion = new Version(appInstaller.Version); diff --git a/src/Files.App/Utils/Serialization/Implementation/DefaultSettingsSerializer.cs b/src/Files.App/Utils/Serialization/Implementation/DefaultSettingsSerializer.cs index 9a3cf4a30804..db37b20cd7f5 100644 --- a/src/Files.App/Utils/Serialization/Implementation/DefaultSettingsSerializer.cs +++ b/src/Files.App/Utils/Serialization/Implementation/DefaultSettingsSerializer.cs @@ -36,14 +36,14 @@ public bool CreateFile(string path) /// public string ReadFromFile() { - _ = _filePath ?? throw new ArgumentNullException(nameof(_filePath)); + ArgumentNullException.ThrowIfNull(_filePath); return ReadStringFromFile(_filePath); } public bool WriteToFile(string? text) { - _ = _filePath ?? throw new ArgumentNullException(nameof(_filePath)); + ArgumentNullException.ThrowIfNull(_filePath); return WriteStringToFile(_filePath, text); } diff --git a/src/Files.App/Utils/Shell/ShellLibraryFolders.cs b/src/Files.App/Utils/Shell/ShellLibraryFolders.cs index a4499c245c98..ee33126d09c3 100644 --- a/src/Files.App/Utils/Shell/ShellLibraryFolders.cs +++ b/src/Files.App/Utils/Shell/ShellLibraryFolders.cs @@ -38,8 +38,7 @@ bool ICollection.IsReadOnly /// location public void Add(ShellItem location) { - if (location is null) - throw new ArgumentNullException(nameof(location)); + ArgumentNullException.ThrowIfNull(location); _lib.AddFolder(location.IShellItem); } @@ -52,8 +51,7 @@ public void Add(ShellItem location) /// location public bool Remove(ShellItem location) { - if (location is null) - throw new ArgumentNullException(nameof(location)); + ArgumentNullException.ThrowIfNull(location); try { diff --git a/src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs b/src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs index 0614a192fdd1..9c0c2d6677ca 100644 --- a/src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs +++ b/src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs @@ -47,10 +47,7 @@ public ZipStorageFolder(string path, string containerPath, ArchiveFileInfo entry => DateCreated = entry.CreationTime == DateTime.MinValue ? DateTimeOffset.MinValue : entry.CreationTime; public ZipStorageFolder(BaseStorageFile backingFile) { - if (string.IsNullOrEmpty(backingFile.Path)) - { - throw new ArgumentException("Backing file Path cannot be null"); - } + ArgumentException.ThrowIfNullOrEmpty(backingFile.Path); Name = IO.Path.GetFileName(backingFile.Path.TrimEnd('\\', '/')); Path = backingFile.Path; this.containerPath = backingFile.Path; diff --git a/src/Files.Shared/Extensions/StringExtensions.cs b/src/Files.Shared/Extensions/StringExtensions.cs index d123b0241f54..38829885fd73 100644 --- a/src/Files.Shared/Extensions/StringExtensions.cs +++ b/src/Files.Shared/Extensions/StringExtensions.cs @@ -13,14 +13,8 @@ public static class StringExtensions /// The substring. public static string Left(this string value, int length) { - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } - if (length < 0) - { - throw new ArgumentOutOfRangeException(nameof(length), length, "Length is less than zero"); - } + ArgumentNullException.ThrowIfNull(value); + ArgumentOutOfRangeException.ThrowIfLessThan(length, 0); return length > value.Length ? value : value.Substring(0, length); } @@ -31,16 +25,10 @@ public static string Left(this string value, int length) /// The substring. public static string Right(this string value, int length) { - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } - if (length < 0) - { - throw new ArgumentOutOfRangeException(nameof(length), length, "Length is less than zero"); - } + ArgumentNullException.ThrowIfNull(value); + ArgumentOutOfRangeException.ThrowIfLessThan(length, 0); - return length > value.Length ? value : value.Substring(value.Length - length); + return length > value.Length ? value : value[^length..]; } } }