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:
@@ -18,7 +18,7 @@ public sealed partial class AnonymousCommand : InvokableCommand
|
||||
|
||||
public override ICommandResult Invoke()
|
||||
{
|
||||
if (_action != null)
|
||||
if (_action is not null)
|
||||
{
|
||||
_action();
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public sealed class ChoiceSetSetting : Setting<string>
|
||||
public override void Update(JsonObject payload)
|
||||
{
|
||||
// If the key doesn't exist in the payload, don't do anything
|
||||
if (payload[Key] != null)
|
||||
if (payload[Key] is not null)
|
||||
{
|
||||
Value = payload[Key]?.GetValue<string>();
|
||||
}
|
||||
|
||||
@@ -2,10 +2,7 @@
|
||||
// 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.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
@@ -293,7 +290,7 @@ public static partial class ClipboardHelper
|
||||
thread.Start();
|
||||
thread.Join();
|
||||
|
||||
if (exception != null)
|
||||
if (exception is not null)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public partial class CommandContextItem : CommandItem, ICommandContextItem
|
||||
c.Name = name;
|
||||
}
|
||||
|
||||
if (result != null)
|
||||
if (result is not null)
|
||||
{
|
||||
c.Result = result;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public partial class CommandItem : BaseObservable, ICommandItem
|
||||
get => _command;
|
||||
set
|
||||
{
|
||||
if (_commandListener != null)
|
||||
if (_commandListener is not null)
|
||||
{
|
||||
_commandListener.Detach();
|
||||
_commandListener = null;
|
||||
@@ -56,7 +56,7 @@ public partial class CommandItem : BaseObservable, ICommandItem
|
||||
|
||||
_command = value;
|
||||
|
||||
if (value != null)
|
||||
if (value is not null)
|
||||
{
|
||||
_commandListener = new(this, OnCommandPropertyChanged, listener => value.PropChanged -= listener.OnEvent);
|
||||
value.PropChanged += _commandListener.OnEvent;
|
||||
@@ -123,7 +123,7 @@ public partial class CommandItem : BaseObservable, ICommandItem
|
||||
c.Name = name;
|
||||
}
|
||||
|
||||
if (result != null)
|
||||
if (result is not null)
|
||||
{
|
||||
c.Result = result;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public partial class ExtensionHost
|
||||
/// <param name="message">The log message to send</param>
|
||||
public static void LogMessage(ILogMessage message)
|
||||
{
|
||||
if (Host != null)
|
||||
if (Host is not null)
|
||||
{
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
@@ -42,7 +42,7 @@ public partial class ExtensionHost
|
||||
|
||||
public static void ShowStatus(IStatusMessage message, StatusContext context)
|
||||
{
|
||||
if (Host != null)
|
||||
if (Host is not null)
|
||||
{
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
@@ -59,7 +59,7 @@ public partial class ExtensionHost
|
||||
|
||||
public static void HideStatus(IStatusMessage message)
|
||||
{
|
||||
if (Host != null)
|
||||
if (Host is not null)
|
||||
{
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
|
||||
@@ -55,7 +55,7 @@ internal sealed partial class ExtensionInstanceManager : IClassFactory
|
||||
|
||||
ppvObject = IntPtr.Zero;
|
||||
|
||||
if (pUnkOuter != null)
|
||||
if (pUnkOuter is not null)
|
||||
{
|
||||
Marshal.ThrowExceptionForHR(CLASS_E_NOAGGREGATION);
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ public abstract class JsonSettingsManager
|
||||
{
|
||||
foreach (var item in newSettings)
|
||||
{
|
||||
savedSettings[item.Key] = item.Value != null ? item.Value.DeepClone() : null;
|
||||
savedSettings[item.Key] = item.Value is not null ? item.Value.DeepClone() : null;
|
||||
}
|
||||
|
||||
var serialized = savedSettings.ToJsonString(_serializerOptions);
|
||||
|
||||
@@ -41,7 +41,7 @@ public sealed partial class Settings : ICommandSettings
|
||||
.Values
|
||||
.Where(s => s is ISettingsForm)
|
||||
.Select(s => s as ISettingsForm)
|
||||
.Where(s => s != null)
|
||||
.Where(s => s is not null)
|
||||
.Select(s => s!);
|
||||
|
||||
var bodies = string.Join(",", settings
|
||||
@@ -77,7 +77,7 @@ public sealed partial class Settings : ICommandSettings
|
||||
.Values
|
||||
.Where(s => s is ISettingsForm)
|
||||
.Select(s => s as ISettingsForm)
|
||||
.Where(s => s != null)
|
||||
.Where(s => s is not null)
|
||||
.Select(s => s!);
|
||||
var content = string.Join(",\n", settings.Select(s => s.ToState()));
|
||||
return $"{{\n{content}\n}}";
|
||||
@@ -86,7 +86,7 @@ public sealed partial class Settings : ICommandSettings
|
||||
public void Update(string data)
|
||||
{
|
||||
var formInput = JsonNode.Parse(data)?.AsObject();
|
||||
if (formInput == null)
|
||||
if (formInput is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public partial class SettingsForm : FormContent
|
||||
public override ICommandResult SubmitForm(string inputs, string data)
|
||||
{
|
||||
var formInput = JsonNode.Parse(inputs)?.AsObject();
|
||||
if (formInput == null)
|
||||
if (formInput is null)
|
||||
{
|
||||
return CommandResult.KeepOpen();
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ public static class ShellHelpers
|
||||
else
|
||||
{
|
||||
var values = Environment.GetEnvironmentVariable("PATH");
|
||||
if (values != null)
|
||||
if (values is not null)
|
||||
{
|
||||
foreach (var path in values.Split(';'))
|
||||
{
|
||||
|
||||
@@ -93,7 +93,7 @@ public partial class StringMatcher
|
||||
|
||||
query = query.Trim();
|
||||
|
||||
// if (_alphabet != null)
|
||||
// if (_alphabet is not null)
|
||||
// {
|
||||
// query = _alphabet.Translate(query);
|
||||
// stringToCompare = _alphabet.Translate(stringToCompare);
|
||||
|
||||
@@ -50,7 +50,7 @@ public partial class TextSetting : Setting<string>
|
||||
public override void Update(JsonObject payload)
|
||||
{
|
||||
// If the key doesn't exist in the payload, don't do anything
|
||||
if (payload[Key] != null)
|
||||
if (payload[Key] is not null)
|
||||
{
|
||||
Value = payload[Key]?.GetValue<string>();
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public sealed class ToggleSetting : Setting<bool>
|
||||
public override void Update(JsonObject payload)
|
||||
{
|
||||
// If the key doesn't exist in the payload, don't do anything
|
||||
if (payload[Key] != null)
|
||||
if (payload[Key] is not null)
|
||||
{
|
||||
// Adaptive cards returns boolean values as a string "true"/"false", cause of course.
|
||||
var strFromJson = payload[Key]?.GetValue<string>() ?? string.Empty;
|
||||
|
||||
Reference in New Issue
Block a user