mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +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:
@@ -6,9 +6,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using ManagedCommon;
|
||||
using Microsoft.CmdPal.Ext.Apps.Utils;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.Storage.Packaging.Appx;
|
||||
using Windows.Win32.System.Com;
|
||||
|
||||
@@ -51,14 +49,14 @@ public static class AppxPackageHelper
|
||||
{
|
||||
result.Add((IntPtr)manifestApp);
|
||||
}
|
||||
else if (manifestApp != null)
|
||||
else if (manifestApp is not null)
|
||||
{
|
||||
manifestApp->Release();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (manifestApp != null)
|
||||
if (manifestApp is not null)
|
||||
{
|
||||
manifestApp->Release();
|
||||
}
|
||||
|
||||
@@ -22,11 +22,11 @@ public class PackageManagerWrapper : IPackageManager
|
||||
{
|
||||
var user = WindowsIdentity.GetCurrent().User;
|
||||
|
||||
if (user != null)
|
||||
if (user is not null)
|
||||
{
|
||||
var pkgs = _packageManager.FindPackagesForUser(user.Value);
|
||||
|
||||
return pkgs.Select(PackageWrapper.GetWrapperFromPackage).Where(package => package != null);
|
||||
return pkgs.Select(PackageWrapper.GetWrapperFromPackage).Where(package => package is not null);
|
||||
}
|
||||
|
||||
return Enumerable.Empty<IPackage>();
|
||||
|
||||
@@ -11,9 +11,7 @@ using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
using ManagedCommon;
|
||||
using Microsoft.CmdPal.Ext.Apps.Utils;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.Storage.Packaging.Appx;
|
||||
using Windows.Win32.System.Com;
|
||||
|
||||
@@ -99,7 +97,7 @@ public partial class UWP
|
||||
private static string[] XmlNamespaces(string path)
|
||||
{
|
||||
var z = XDocument.Load(path);
|
||||
if (z.Root != null)
|
||||
if (z.Root is not null)
|
||||
{
|
||||
var namespaces = z.Root.Attributes().
|
||||
Where(a => a.IsNamespaceDeclaration).
|
||||
|
||||
@@ -308,7 +308,7 @@ public class UWPApplication : IProgram
|
||||
private bool SetScaleIcons(string path, string colorscheme, bool highContrast = false)
|
||||
{
|
||||
var extension = Path.GetExtension(path);
|
||||
if (extension != null)
|
||||
if (extension is not null)
|
||||
{
|
||||
var end = path.Length - extension.Length;
|
||||
var prefix = path.Substring(0, end);
|
||||
@@ -363,7 +363,7 @@ public class UWPApplication : IProgram
|
||||
private bool SetTargetSizeIcon(string path, string colorscheme, bool highContrast = false)
|
||||
{
|
||||
var extension = Path.GetExtension(path);
|
||||
if (extension != null)
|
||||
if (extension is not null)
|
||||
{
|
||||
var end = path.Length - extension.Length;
|
||||
var prefix = path.Substring(0, end);
|
||||
@@ -576,7 +576,7 @@ public class UWPApplication : IProgram
|
||||
|
||||
var group = new DrawingGroup();
|
||||
var converted = ColorConverter.ConvertFromString(currentBackgroundColor);
|
||||
if (converted != null)
|
||||
if (converted is not null)
|
||||
{
|
||||
var color = (Color)converted;
|
||||
var brush = new SolidColorBrush(color);
|
||||
|
||||
@@ -170,7 +170,7 @@ public class Win32Program : IProgram
|
||||
|
||||
public bool QueryEqualsNameForRunCommands(string query)
|
||||
{
|
||||
if (query != null && AppType == ApplicationType.RunCommand)
|
||||
if (query is not null && AppType == ApplicationType.RunCommand)
|
||||
{
|
||||
// Using OrdinalIgnoreCase since this is used internally
|
||||
if (!query.Equals(Name, StringComparison.OrdinalIgnoreCase) && !query.Equals(ExecutableName, StringComparison.OrdinalIgnoreCase))
|
||||
@@ -667,7 +667,7 @@ public class Win32Program : IProgram
|
||||
var paths = new List<string>();
|
||||
using (var root = Registry.LocalMachine.OpenSubKey(appPaths))
|
||||
{
|
||||
if (root != null)
|
||||
if (root is not null)
|
||||
{
|
||||
paths.AddRange(GetPathsFromRegistry(root));
|
||||
}
|
||||
@@ -675,7 +675,7 @@ public class Win32Program : IProgram
|
||||
|
||||
using (var root = Registry.CurrentUser.OpenSubKey(appPaths))
|
||||
{
|
||||
if (root != null)
|
||||
if (root is not null)
|
||||
{
|
||||
paths.AddRange(GetPathsFromRegistry(root));
|
||||
}
|
||||
@@ -700,7 +700,7 @@ public class Win32Program : IProgram
|
||||
{
|
||||
using (var key = root.OpenSubKey(subkey))
|
||||
{
|
||||
if (key == null)
|
||||
if (key is null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
@@ -742,13 +742,13 @@ public class Win32Program : IProgram
|
||||
|
||||
public bool Equals(Win32Program? app1, Win32Program? app2)
|
||||
{
|
||||
if (app1 == null && app2 == null)
|
||||
if (app1 is null && app2 is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return app1 != null
|
||||
&& app2 != null
|
||||
return app1 is not null
|
||||
&& app2 is not null
|
||||
&& (app1.Name?.ToUpperInvariant(), app1.ExecutableName?.ToUpperInvariant(), app1.FullPath?.ToUpperInvariant())
|
||||
.Equals((app2.Name?.ToUpperInvariant(), app2.ExecutableName?.ToUpperInvariant(), app2.FullPath?.ToUpperInvariant()));
|
||||
}
|
||||
@@ -908,7 +908,7 @@ public class Win32Program : IProgram
|
||||
Parallel.ForEach(paths, source =>
|
||||
{
|
||||
var program = GetProgramFromPath(source);
|
||||
if (program != null)
|
||||
if (program is not null)
|
||||
{
|
||||
programsList.Add(program);
|
||||
}
|
||||
@@ -918,7 +918,7 @@ public class Win32Program : IProgram
|
||||
Parallel.ForEach(runCommandPaths, source =>
|
||||
{
|
||||
var program = GetRunCommandProgramFromPath(source);
|
||||
if (program != null)
|
||||
if (program is not null)
|
||||
{
|
||||
runCommandProgramsList.Add(program);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user