From 288aa1987c3fccf4e13ee9b7f373d0bef75e9e1b Mon Sep 17 00:00:00 2001 From: Corvin Date: Tue, 8 Jul 2025 20:45:42 +0200 Subject: [PATCH 1/2] use newer sytaxes/APIs --- src/Files.App/Extensions/EnumExtensions.cs | 2 +- .../Helpers/WMI/ManagementEventWatcher.cs | 30 ++++--------------- .../Services/App/AppUpdateSideloadService.cs | 3 +- .../DefaultSettingsSerializer.cs | 4 +-- .../Utils/Shell/ShellLibraryFolders.cs | 6 ++-- .../Extensions/StringExtensions.cs | 22 ++++---------- 6 files changed, 17 insertions(+), 50 deletions(-) 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.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..]; } } } From a2abf2a0447f1ec6fe733c7d716e9633c9531451 Mon Sep 17 00:00:00 2001 From: Corvin Date: Wed, 9 Jul 2025 18:11:48 +0200 Subject: [PATCH 2/2] replaced another ArgumentException.ThrowIfNullOrEmpty --- src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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;