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:
Michael Jolley
2025-08-18 06:07:28 -05:00
committed by GitHub
parent efb48aa163
commit 6acb793184
138 changed files with 395 additions and 431 deletions

View File

@@ -18,10 +18,10 @@ internal sealed partial class SwitchToWindowCommand : InvokableCommand
{
Name = Resources.switch_to_command_title;
_window = window;
if (_window != null)
if (_window is not null)
{
var p = Process.GetProcessById((int)_window.Process.ProcessID);
if (p != null)
if (p is not null)
{
try
{

View File

@@ -23,7 +23,7 @@ internal static class ResultHelper
/// <returns>List of results</returns>
internal static List<WindowWalkerListItem> GetResultList(List<SearchResult> searchControllerResults, bool isKeywordSearch)
{
if (searchControllerResults == null || searchControllerResults.Count == 0)
if (searchControllerResults is null || searchControllerResults.Count == 0)
{
return [];
}

View File

@@ -83,7 +83,7 @@ public class VirtualDesktopHelper
/// <summary>
/// Gets a value indicating whether the Virtual Desktop Manager is initialized successfully
/// </summary>
public bool VirtualDesktopManagerInitialized => _virtualDesktopManager != null;
public bool VirtualDesktopManagerInitialized => _virtualDesktopManager is not null;
/// <summary>
/// Method to update the list of Virtual Desktops from Registry
@@ -98,10 +98,10 @@ public class VirtualDesktopHelper
// List of all desktops
using RegistryKey? virtualDesktopKey = Registry.CurrentUser.OpenSubKey(registryExplorerVirtualDesktops, false);
if (virtualDesktopKey != null)
if (virtualDesktopKey is not null)
{
var allDeskValue = (byte[]?)virtualDesktopKey.GetValue("VirtualDesktopIDs", null) ?? Array.Empty<byte>();
if (allDeskValue != null)
if (allDeskValue is not null)
{
// We clear only, if we can read from registry. Otherwise, we keep the existing values.
_availableDesktops.Clear();
@@ -124,10 +124,10 @@ public class VirtualDesktopHelper
// Guid for current desktop
var virtualDesktopsKeyName = _isWindowsEleven ? registryExplorerVirtualDesktops : registrySessionVirtualDesktops;
using RegistryKey? virtualDesktopsKey = Registry.CurrentUser.OpenSubKey(virtualDesktopsKeyName, false);
if (virtualDesktopsKey != null)
if (virtualDesktopsKey is not null)
{
var currentVirtualDesktopValue = virtualDesktopsKey.GetValue("CurrentVirtualDesktop", null);
if (currentVirtualDesktopValue != null)
if (currentVirtualDesktopValue is not null)
{
_currentDesktop = new Guid((byte[])currentVirtualDesktopValue);
}
@@ -268,7 +268,7 @@ public class VirtualDesktopHelper
using RegistryKey? deskSubKey = Registry.CurrentUser.OpenSubKey(registryPath, false);
var desktopName = deskSubKey?.GetValue("Name");
return (desktopName != null) ? (string)desktopName : defaultName;
return (desktopName is not null) ? (string)desktopName : defaultName;
}
/// <summary>
@@ -313,7 +313,7 @@ public class VirtualDesktopHelper
/// <returns>HResult of the called method as integer.</returns>
public int GetWindowDesktopId(IntPtr hWindow, out Guid desktopId)
{
if (_virtualDesktopManager == null)
if (_virtualDesktopManager is null)
{
ExtensionHost.LogMessage(new LogMessage() { Message = "VirtualDesktopHelper.GetWindowDesktopId() failed: The instance of <IVirtualDesktopHelper> isn't available." });
desktopId = Guid.Empty;
@@ -330,7 +330,7 @@ public class VirtualDesktopHelper
/// <returns>An instance of <see cref="VDesktop"/> for the desktop where the window is assigned to, or an empty instance of <see cref="VDesktop"/> on failure.</returns>
public VDesktop GetWindowDesktop(IntPtr hWindow)
{
if (_virtualDesktopManager == null)
if (_virtualDesktopManager is null)
{
ExtensionHost.LogMessage(new LogMessage() { Message = "VirtualDesktopHelper.GetWindowDesktop() failed: The instance of <IVirtualDesktopHelper> isn't available." });
return CreateVDesktopInstance(Guid.Empty);
@@ -348,7 +348,7 @@ public class VirtualDesktopHelper
/// <returns>Type of <see cref="VirtualDesktopAssignmentType"/>.</returns>
public VirtualDesktopAssignmentType GetWindowDesktopAssignmentType(IntPtr hWindow, Guid? desktop = null)
{
if (_virtualDesktopManager == null)
if (_virtualDesktopManager is null)
{
ExtensionHost.LogMessage(new LogMessage() { Message = "VirtualDesktopHelper.GetWindowDesktopAssignmentType() failed: The instance of <IVirtualDesktopHelper> isn't available." });
return VirtualDesktopAssignmentType.Unknown;
@@ -415,7 +415,7 @@ public class VirtualDesktopHelper
/// <returns><see langword="True"/> on success and <see langword="false"/> on failure.</returns>
public bool MoveWindowToDesktop(IntPtr hWindow, ref Guid desktopId)
{
if (_virtualDesktopManager == null)
if (_virtualDesktopManager is null)
{
ExtensionHost.LogMessage(new LogMessage() { Message = "VirtualDesktopHelper.MoveWindowToDesktop() failed: The instance of <IVirtualDesktopHelper> isn't available." });
return false;