Skip to content

Commit 6b8a578

Browse files
authored
Code Quality: Use newer syntaxes for ArgumentException handling (#17258)
1 parent 8313457 commit 6b8a578

File tree

7 files changed

+18
-54
lines changed

7 files changed

+18
-54
lines changed

src/Files.App/Extensions/EnumExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static TEnum GetEnum<TEnum>(string text) where TEnum : struct
1515
throw new InvalidOperationException("Generic parameter 'TEnum' must be an enum.");
1616
}
1717

18-
return (TEnum)Enum.Parse(typeof(TEnum), text);
18+
return Enum.Parse<TEnum>(text);
1919
}
2020
}
2121
}

src/Files.App/Helpers/WMI/ManagementEventWatcher.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public ManagementEventWatcher(WqlEventQuery query)
5151
{
5252
string queryExpression = query.QueryExpression;
5353

54-
if (string.IsNullOrWhiteSpace(queryExpression))
55-
{
56-
throw new ArgumentNullException(nameof(queryExpression));
57-
}
54+
ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression);
5855

5956
_nameSpace = DefaultNameSpace;
6057
_queryDialect = DefaultQueryDialect;
@@ -70,10 +67,7 @@ public ManagementEventWatcher(WqlEventQuery query)
7067
/// <param name="queryExpression"></param>
7168
public ManagementEventWatcher(string queryDialect, string queryExpression)
7269
{
73-
if (string.IsNullOrWhiteSpace(queryExpression))
74-
{
75-
throw new ArgumentNullException(nameof(queryExpression));
76-
}
70+
ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression);
7771

7872
_nameSpace = DefaultNameSpace;
7973
_queryDialect = queryDialect ?? DefaultQueryDialect;
@@ -90,10 +84,7 @@ public ManagementEventWatcher(string queryDialect, string queryExpression)
9084
/// <param name="queryExpression"></param>
9185
public ManagementEventWatcher(string nameSpace, string queryDialect, string queryExpression)
9286
{
93-
if (string.IsNullOrWhiteSpace(queryExpression))
94-
{
95-
throw new ArgumentNullException(nameof(queryExpression));
96-
}
87+
ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression);
9788

9889
_nameSpace = nameSpace ?? DefaultNameSpace;
9990
_queryDialect = queryDialect ?? DefaultQueryDialect;
@@ -111,10 +102,7 @@ public ManagementEventWatcher(string nameSpace, string queryDialect, string quer
111102
/// <param name="queryExpression"></param>
112103
public ManagementEventWatcher(string computerName, string nameSpace, string queryDialect, string queryExpression)
113104
{
114-
if (string.IsNullOrWhiteSpace(queryExpression))
115-
{
116-
throw new ArgumentNullException(nameof(queryExpression));
117-
}
105+
ArgumentNullException.ThrowIfNullOrWhiteSpace(queryExpression);
118106

119107
_computerName = computerName;
120108
_nameSpace = nameSpace ?? DefaultNameSpace;
@@ -160,10 +148,7 @@ public void Start()
160148
{
161149
lock (_myLock)
162150
{
163-
if (_isDisposed)
164-
{
165-
throw new ObjectDisposedException(nameof(ManagementEventWatcher));
166-
}
151+
ObjectDisposedException.ThrowIf(_isDisposed, this);
167152

168153
if (_cimWatcherStatus != CimWatcherStatus.Default && _cimWatcherStatus != CimWatcherStatus.Stopped)
169154
{
@@ -180,10 +165,7 @@ public void Stop()
180165
{
181166
lock (_myLock)
182167
{
183-
if (_isDisposed)
184-
{
185-
throw new ObjectDisposedException(nameof(ManagementEventWatcher));
186-
}
168+
ObjectDisposedException.ThrowIf(_isDisposed, this);
187169

188170
if (_cimWatcherStatus != CimWatcherStatus.Started)
189171
{

src/Files.App/Services/App/AppUpdateSideloadService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public async Task CheckForUpdatesAsync()
8787
XmlSerializer xml = new XmlSerializer(typeof(AppInstaller));
8888
var appInstaller = (AppInstaller?)xml.Deserialize(stream);
8989

90-
if (appInstaller is null)
91-
throw new ArgumentNullException(nameof(appInstaller));
90+
ArgumentNullException.ThrowIfNull(appInstaller);
9291

9392
var remoteVersion = new Version(appInstaller.Version);
9493

src/Files.App/Utils/Serialization/Implementation/DefaultSettingsSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public bool CreateFile(string path)
3636
/// <exception cref="ArgumentNullException"></exception>
3737
public string ReadFromFile()
3838
{
39-
_ = _filePath ?? throw new ArgumentNullException(nameof(_filePath));
39+
ArgumentNullException.ThrowIfNull(_filePath);
4040

4141
return ReadStringFromFile(_filePath);
4242
}
4343

4444
public bool WriteToFile(string? text)
4545
{
46-
_ = _filePath ?? throw new ArgumentNullException(nameof(_filePath));
46+
ArgumentNullException.ThrowIfNull(_filePath);
4747

4848
return WriteStringToFile(_filePath, text);
4949
}

src/Files.App/Utils/Shell/ShellLibraryFolders.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ bool ICollection<ShellItem>.IsReadOnly
3838
/// <exception cref="ArgumentNullException">location</exception>
3939
public void Add(ShellItem location)
4040
{
41-
if (location is null)
42-
throw new ArgumentNullException(nameof(location));
41+
ArgumentNullException.ThrowIfNull(location);
4342

4443
_lib.AddFolder(location.IShellItem);
4544
}
@@ -52,8 +51,7 @@ public void Add(ShellItem location)
5251
/// <exception cref="ArgumentNullException">location</exception>
5352
public bool Remove(ShellItem location)
5453
{
55-
if (location is null)
56-
throw new ArgumentNullException(nameof(location));
54+
ArgumentNullException.ThrowIfNull(location);
5755

5856
try
5957
{

src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ public ZipStorageFolder(string path, string containerPath, ArchiveFileInfo entry
4747
=> DateCreated = entry.CreationTime == DateTime.MinValue ? DateTimeOffset.MinValue : entry.CreationTime;
4848
public ZipStorageFolder(BaseStorageFile backingFile)
4949
{
50-
if (string.IsNullOrEmpty(backingFile.Path))
51-
{
52-
throw new ArgumentException("Backing file Path cannot be null");
53-
}
50+
ArgumentException.ThrowIfNullOrEmpty(backingFile.Path);
5451
Name = IO.Path.GetFileName(backingFile.Path.TrimEnd('\\', '/'));
5552
Path = backingFile.Path;
5653
this.containerPath = backingFile.Path;

src/Files.Shared/Extensions/StringExtensions.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ public static class StringExtensions
1313
/// <returns>The substring.</returns>
1414
public static string Left(this string value, int length)
1515
{
16-
if (value is null)
17-
{
18-
throw new ArgumentNullException(nameof(value));
19-
}
20-
if (length < 0)
21-
{
22-
throw new ArgumentOutOfRangeException(nameof(length), length, "Length is less than zero");
23-
}
16+
ArgumentNullException.ThrowIfNull(value);
17+
ArgumentOutOfRangeException.ThrowIfLessThan(length, 0);
2418

2519
return length > value.Length ? value : value.Substring(0, length);
2620
}
@@ -31,16 +25,10 @@ public static string Left(this string value, int length)
3125
/// <returns>The substring.</returns>
3226
public static string Right(this string value, int length)
3327
{
34-
if (value is null)
35-
{
36-
throw new ArgumentNullException(nameof(value));
37-
}
38-
if (length < 0)
39-
{
40-
throw new ArgumentOutOfRangeException(nameof(length), length, "Length is less than zero");
41-
}
28+
ArgumentNullException.ThrowIfNull(value);
29+
ArgumentOutOfRangeException.ThrowIfLessThan(length, 0);
4230

43-
return length > value.Length ? value : value.Substring(value.Length - length);
31+
return length > value.Length ? value : value[^length..];
4432
}
4533
}
4634
}

0 commit comments

Comments
 (0)