Skip to content

Code Quality: Use newer syntaxes for ArgumentException handling #17258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Files.App/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static TEnum GetEnum<TEnum>(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<TEnum>(text);
}
}
}
30 changes: 6 additions & 24 deletions src/Files.App/Helpers/WMI/ManagementEventWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -70,10 +67,7 @@ public ManagementEventWatcher(WqlEventQuery query)
/// <param name="queryExpression"></param>
public ManagementEventWatcher(string queryDialect, string queryExpression)
{
if (string.IsNullOrWhiteSpace(queryExpression))
{
throw new ArgumentNullException(nameof(queryExpression));
}
ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression);

_nameSpace = DefaultNameSpace;
_queryDialect = queryDialect ?? DefaultQueryDialect;
Expand All @@ -90,10 +84,7 @@ public ManagementEventWatcher(string queryDialect, string queryExpression)
/// <param name="queryExpression"></param>
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;
Expand All @@ -111,10 +102,7 @@ public ManagementEventWatcher(string nameSpace, string queryDialect, string quer
/// <param name="queryExpression"></param>
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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -180,10 +165,7 @@ public void Stop()
{
lock (_myLock)
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(ManagementEventWatcher));
}
ObjectDisposedException.ThrowIf(_isDisposed, this);

if (_cimWatcherStatus != CimWatcherStatus.Started)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Files.App/Services/App/AppUpdateSideloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public bool CreateFile(string path)
/// <exception cref="ArgumentNullException"></exception>
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);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Files.App/Utils/Shell/ShellLibraryFolders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ bool ICollection<ShellItem>.IsReadOnly
/// <exception cref="ArgumentNullException">___location</exception>
public void Add(ShellItem ___location)
{
if (___location is null)
throw new ArgumentNullException(nameof(___location));
ArgumentNullException.ThrowIfNull(___location);

_lib.AddFolder(___location.IShellItem);
}
Expand All @@ -52,8 +51,7 @@ public void Add(ShellItem ___location)
/// <exception cref="ArgumentNullException">___location</exception>
public bool Remove(ShellItem ___location)
{
if (___location is null)
throw new ArgumentNullException(nameof(___location));
ArgumentNullException.ThrowIfNull(___location);

try
{
Expand Down
5 changes: 1 addition & 4 deletions src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 5 additions & 17 deletions src/Files.Shared/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ public static class StringExtensions
/// <returns>The substring.</returns>
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);
}
Expand All @@ -31,16 +25,10 @@ public static string Left(this string value, int length)
/// <returns>The substring.</returns>
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..];
}
}
}
Loading