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:
@@ -2,16 +2,12 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ManagedCommon;
|
||||
using Microsoft.CmdPal.Ext.Apps.Commands;
|
||||
using Microsoft.CmdPal.Ext.Apps.Programs;
|
||||
using Microsoft.CmdPal.Ext.Apps.Properties;
|
||||
using Microsoft.CmdPal.Ext.Apps.State;
|
||||
@@ -145,7 +141,7 @@ public sealed partial class AllAppsPage : ListPage
|
||||
*/
|
||||
var existingAppItem = allApps.FirstOrDefault(f => f.AppIdentifier == e.AppIdentifier);
|
||||
|
||||
if (existingAppItem != null)
|
||||
if (existingAppItem is not null)
|
||||
{
|
||||
var appListItem = new AppListItem(existingAppItem, true, e.IsPinned);
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CmdPal.Ext.Apps.Programs;
|
||||
@@ -66,7 +65,7 @@ public sealed partial class AppCache : IDisposable
|
||||
|
||||
private void UpdateUWPIconPath(Theme theme)
|
||||
{
|
||||
if (_packageRepository != null)
|
||||
if (_packageRepository is not null)
|
||||
{
|
||||
foreach (UWPApplication app in _packageRepository)
|
||||
{
|
||||
|
||||
@@ -72,7 +72,7 @@ internal sealed partial class AppListItem : ListItem
|
||||
try
|
||||
{
|
||||
var stream = await ThumbnailHelper.GetThumbnail(_app.ExePath, true);
|
||||
if (stream != null)
|
||||
if (stream is not null)
|
||||
{
|
||||
heroImage = IconInfo.FromStream(stream);
|
||||
}
|
||||
@@ -106,7 +106,7 @@ internal sealed partial class AppListItem : ListItem
|
||||
try
|
||||
{
|
||||
var stream = await ThumbnailHelper.GetThumbnail(_app.ExePath);
|
||||
if (stream != null)
|
||||
if (stream is not null)
|
||||
{
|
||||
var data = new IconData(RandomAccessStreamReference.CreateFromStream(stream));
|
||||
icon = new IconInfo(data, data);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public sealed partial class FileSystemWatcherWrapper : FileSystemWatcher, IFileS
|
||||
get => this.Filters;
|
||||
set
|
||||
{
|
||||
if (value != null)
|
||||
if (value is not null)
|
||||
{
|
||||
foreach (var filter in value)
|
||||
{
|
||||
|
||||
@@ -10,7 +10,6 @@ using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using ManagedCommon;
|
||||
using Microsoft.CmdPal.Ext.Apps.Programs;
|
||||
using Win32Program = Microsoft.CmdPal.Ext.Apps.Programs.Win32Program;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Apps.Storage;
|
||||
@@ -53,7 +52,7 @@ internal sealed partial class Win32ProgramRepository : ListRepository<Programs.W
|
||||
if (!string.IsNullOrEmpty(appPath))
|
||||
{
|
||||
Win32Program? app = Win32Program.GetAppFromPath(appPath);
|
||||
if (app != null)
|
||||
if (app is not null)
|
||||
{
|
||||
Add(app);
|
||||
_isDirty = true;
|
||||
@@ -137,7 +136,7 @@ internal sealed partial class Win32ProgramRepository : ListRepository<Programs.W
|
||||
}
|
||||
|
||||
// To remove the old app which has been renamed and to add the new application.
|
||||
if (oldApp != null)
|
||||
if (oldApp is not null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(oldApp.Name) || string.IsNullOrWhiteSpace(oldApp.ExecutableName) || string.IsNullOrWhiteSpace(oldApp.FullPath))
|
||||
{
|
||||
@@ -149,7 +148,7 @@ internal sealed partial class Win32ProgramRepository : ListRepository<Programs.W
|
||||
}
|
||||
}
|
||||
|
||||
if (newApp != null)
|
||||
if (newApp is not null)
|
||||
{
|
||||
Add(newApp);
|
||||
_isDirty = true;
|
||||
@@ -177,7 +176,7 @@ internal sealed partial class Win32ProgramRepository : ListRepository<Programs.W
|
||||
if (extension.Equals(LnkExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
app = GetAppWithSameLnkFilePath(path);
|
||||
if (app == null)
|
||||
if (app is null)
|
||||
{
|
||||
// Cancelled links won't have a resolved path.
|
||||
app = GetAppWithSameNameAndExecutable(Path.GetFileNameWithoutExtension(path), Path.GetFileName(path));
|
||||
@@ -197,7 +196,7 @@ internal sealed partial class Win32ProgramRepository : ListRepository<Programs.W
|
||||
Logger.LogError(ex.Message);
|
||||
}
|
||||
|
||||
if (app != null)
|
||||
if (app is not null)
|
||||
{
|
||||
Remove(app);
|
||||
_isDirty = true;
|
||||
@@ -244,7 +243,7 @@ internal sealed partial class Win32ProgramRepository : ListRepository<Programs.W
|
||||
if (!Path.GetExtension(path).Equals(UrlExtension, StringComparison.OrdinalIgnoreCase) && !Path.GetExtension(path).Equals(LnkExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Programs.Win32Program? app = Programs.Win32Program.GetAppFromPath(path);
|
||||
if (app != null)
|
||||
if (app is not null)
|
||||
{
|
||||
Add(app);
|
||||
_isDirty = true;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.System.Com;
|
||||
@@ -27,7 +26,7 @@ public static class ComFreeHelper
|
||||
public static unsafe void ComObjectRelease<T>(T* comPtr)
|
||||
where T : unmanaged
|
||||
{
|
||||
if (comPtr != null)
|
||||
if (comPtr is not null)
|
||||
{
|
||||
((IUnknown*)comPtr)->Release();
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using ManagedCommon;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.System.Com;
|
||||
@@ -37,7 +35,7 @@ public class ShellLinkHelper : IShellLinkHelper
|
||||
IPersistFile* persistFile = null;
|
||||
Guid iid = typeof(IPersistFile).GUID;
|
||||
((IUnknown*)link)->QueryInterface(&iid, (void**)&persistFile);
|
||||
if (persistFile != null)
|
||||
if (persistFile is not null)
|
||||
{
|
||||
using var persistFileHandle = new SafeComHandle((IntPtr)persistFile);
|
||||
try
|
||||
|
||||
@@ -45,7 +45,7 @@ public class ShellLocalization
|
||||
|
||||
var filename = ComFreeHelper.GetStringAndFree(hr, filenamePtr);
|
||||
|
||||
if (filename == null)
|
||||
if (filename is null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public static class ThemeHelper
|
||||
|
||||
// Retrieve the registry value, which is a DWORD (0 or 1)
|
||||
var registryValueObj = Registry.GetValue(registryKey, registryValue, null);
|
||||
if (registryValueObj != null)
|
||||
if (registryValueObj is not null)
|
||||
{
|
||||
// 0 = Dark mode, 1 = Light mode
|
||||
var isLightMode = Convert.ToBoolean((int)registryValueObj, CultureInfo.InvariantCulture);
|
||||
|
||||
Reference in New Issue
Block a user