mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
CmdPal: Null pattern matching based on is expression rather than overridable operators (#40972)
What the title says. 😄
Rather than relying on the potentially overloaded `!=` or `==` operators
when checking for null, now we'll use the `is` expression (possibly
combined with the `not` operator) to ensure correct checking. Probably
overkill for many of these classes, but decided to err on the side of
consistency. Would matter more on classes that may be inherited or
extended.
Using `is` and `is not` will provide us a guarantee that no
user-overloaded equality operators (`==`/`!=`) is invoked when a
`expression is null` is evaluated.
In code form, changed all instances of:
```c#
something != null
something == null
```
to:
```c#
something is not null
something is null
```
The one exception was checking null on a `KeyChord`. `KeyChord` is a
struct which is never null so VS will raise an error when trying this
versus just providing a warning when using `keyChord != null`. In
reality, we shouldn't do this check because it can't ever be null. In
the case of a `KeyChord` it **would** be a `KeyChord` equivalent to:
```c#
KeyChord keyChord = new ()
{
Modifiers = 0,
Vkey = 0,
ScanCode = 0
};
```
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
@@ -79,7 +78,7 @@ public partial class InstallPackageCommand : InvokableCommand
|
||||
{
|
||||
// TODO: LOCK in here, so this can only be invoked once until the
|
||||
// install / uninstall is done. Just use like, an atomic
|
||||
if (_installTask != null)
|
||||
if (_installTask is not null)
|
||||
{
|
||||
return CommandResult.KeepOpen();
|
||||
}
|
||||
@@ -143,7 +142,7 @@ public partial class InstallPackageCommand : InvokableCommand
|
||||
{
|
||||
await Task.Delay(2500).ConfigureAwait(false);
|
||||
|
||||
if (_installTask == null)
|
||||
if (_installTask is null)
|
||||
{
|
||||
WinGetExtensionHost.Instance.HideStatus(_installBanner);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ public partial class InstallPackageListItem : ListItem
|
||||
|
||||
var version = _package.DefaultInstallVersion ?? _package.InstalledVersion;
|
||||
var versionTagText = "Unknown";
|
||||
if (version != null)
|
||||
if (version is not null)
|
||||
{
|
||||
versionTagText = version.Version == "Unknown" && version.PackageCatalog.Info.Id == "StoreEdgeFD" ? "msstore" : version.Version;
|
||||
}
|
||||
@@ -60,11 +60,11 @@ public partial class InstallPackageListItem : ListItem
|
||||
Logger.LogWarning($"{ex.ErrorCode}");
|
||||
}
|
||||
|
||||
if (metadata != null)
|
||||
if (metadata is not null)
|
||||
{
|
||||
if (metadata.Tags.Where(t => t.Equals(WinGetExtensionPage.ExtensionsTag, StringComparison.OrdinalIgnoreCase)).Any())
|
||||
{
|
||||
if (_installCommand != null)
|
||||
if (_installCommand is not null)
|
||||
{
|
||||
_installCommand.SkipDependencies = true;
|
||||
}
|
||||
@@ -172,7 +172,7 @@ public partial class InstallPackageListItem : ListItem
|
||||
return;
|
||||
}
|
||||
|
||||
var isInstalled = _package.InstalledVersion != null;
|
||||
var isInstalled = _package.InstalledVersion is not null;
|
||||
|
||||
var installedState = isInstalled ?
|
||||
(_package.IsUpdateAvailable ?
|
||||
@@ -193,11 +193,11 @@ public partial class InstallPackageListItem : ListItem
|
||||
Icon = Icons.DeleteIcon,
|
||||
};
|
||||
|
||||
if (WinGetStatics.AppSearchCallback != null)
|
||||
if (WinGetStatics.AppSearchCallback is not null)
|
||||
{
|
||||
var callback = WinGetStatics.AppSearchCallback;
|
||||
var installedApp = callback(_package.DefaultInstallVersion == null ? _package.Name : _package.DefaultInstallVersion.DisplayName);
|
||||
if (installedApp != null)
|
||||
var installedApp = callback(_package.DefaultInstallVersion is null ? _package.Name : _package.DefaultInstallVersion.DisplayName);
|
||||
if (installedApp is not null)
|
||||
{
|
||||
this.Command = installedApp.Command;
|
||||
contextMenu = [.. installedApp.MoreCommands];
|
||||
|
||||
@@ -53,7 +53,7 @@ internal sealed partial class WinGetExtensionPage : DynamicListPage, IDisposable
|
||||
{
|
||||
// emptySearchForTag ===
|
||||
// we don't have results yet, we haven't typed anything, and we're searching for a tag
|
||||
var emptySearchForTag = _results == null &&
|
||||
var emptySearchForTag = _results is null &&
|
||||
string.IsNullOrEmpty(SearchText) &&
|
||||
HasTag;
|
||||
|
||||
@@ -64,7 +64,7 @@ internal sealed partial class WinGetExtensionPage : DynamicListPage, IDisposable
|
||||
return items;
|
||||
}
|
||||
|
||||
if (_results != null && _results.Any())
|
||||
if (_results is not null && _results.Any())
|
||||
{
|
||||
ListItem[] results = _results.Select(PackageToListItem).ToArray();
|
||||
IsLoading = false;
|
||||
@@ -100,7 +100,7 @@ internal sealed partial class WinGetExtensionPage : DynamicListPage, IDisposable
|
||||
private void DoUpdateSearchText(string newSearch)
|
||||
{
|
||||
// Cancel any ongoing search
|
||||
if (_cancellationTokenSource != null)
|
||||
if (_cancellationTokenSource is not null)
|
||||
{
|
||||
Logger.LogDebug("Cancelling old search", memberName: nameof(DoUpdateSearchText));
|
||||
_cancellationTokenSource.Cancel();
|
||||
@@ -221,7 +221,7 @@ internal sealed partial class WinGetExtensionPage : DynamicListPage, IDisposable
|
||||
// WinGetStatics static ctor when we were created.
|
||||
PackageCatalog catalog = await catalogTask.Value;
|
||||
|
||||
if (catalog == null)
|
||||
if (catalog is null)
|
||||
{
|
||||
// This error should have already been displayed by WinGetStatics
|
||||
return [];
|
||||
|
||||
Reference in New Issue
Block a user