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:
@@ -46,7 +46,7 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System
|
||||
return;
|
||||
}
|
||||
|
||||
if (_suppressCallback != null && _suppressCallback(query))
|
||||
if (_suppressCallback is not null && _suppressCallback(query))
|
||||
{
|
||||
Command = new NoOpCommand();
|
||||
Title = string.Empty;
|
||||
@@ -71,7 +71,7 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System
|
||||
try
|
||||
{
|
||||
var stream = ThumbnailHelper.GetThumbnail(item.FullPath).Result;
|
||||
if (stream != null)
|
||||
if (stream is not null)
|
||||
{
|
||||
var data = new IconData(RandomAccessStreamReference.CreateFromStream(stream));
|
||||
Icon = new IconInfo(data, data);
|
||||
@@ -92,7 +92,7 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System
|
||||
_searchEngine.Query(query, _queryCookie);
|
||||
var results = _searchEngine.FetchItems(0, 20, _queryCookie, out var _);
|
||||
|
||||
if (results.Count == 0 || ((results[0] as IndexerListItem) == null))
|
||||
if (results.Count == 0 || ((results[0] as IndexerListItem) is null))
|
||||
{
|
||||
// Exit 2: We searched for the file, and found nothing. Oh well.
|
||||
// Hide ourselves.
|
||||
|
||||
@@ -16,7 +16,7 @@ internal static class DataSourceManager
|
||||
|
||||
public static IDBInitialize GetDataSource()
|
||||
{
|
||||
if (_dataSource == null)
|
||||
if (_dataSource is null)
|
||||
{
|
||||
InitializeDataSource();
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ using ManagedCsWin32;
|
||||
using Microsoft.CmdPal.Ext.Indexer.Indexer.OleDB;
|
||||
using Microsoft.CmdPal.Ext.Indexer.Indexer.SystemSearch;
|
||||
using Microsoft.CmdPal.Ext.Indexer.Indexer.Utils;
|
||||
using static Microsoft.CmdPal.Ext.Indexer.Indexer.Utils.NativeHelpers;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.Indexer.Indexer;
|
||||
|
||||
@@ -54,14 +53,14 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
try
|
||||
{
|
||||
queryTpTimer = new Timer(QueryTimerCallback, this, Timeout.Infinite, Timeout.Infinite);
|
||||
if (queryTpTimer == null)
|
||||
if (queryTpTimer is null)
|
||||
{
|
||||
Logger.LogError("Failed to create query timer");
|
||||
return;
|
||||
}
|
||||
|
||||
queryCompletedEvent = new EventWaitHandle(false, EventResetMode.ManualReset);
|
||||
if (queryCompletedEvent == null)
|
||||
if (queryCompletedEvent is null)
|
||||
{
|
||||
Logger.LogError("Failed to create query completed event");
|
||||
return;
|
||||
@@ -85,7 +84,7 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
// Are we currently doing work? If so, let's cancel
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (queryTpTimer != null)
|
||||
if (queryTpTimer is not null)
|
||||
{
|
||||
queryTpTimer.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
queryTpTimer.Dispose();
|
||||
@@ -117,7 +116,7 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
try
|
||||
{
|
||||
// We need to generate a search query string with the search text the user entered above
|
||||
if (currentRowset != null)
|
||||
if (currentRowset is not null)
|
||||
{
|
||||
// We have a previous rowset, this means the user is typing and we should store this
|
||||
// recapture the where ID from this so the next ExecuteSync call will be faster
|
||||
@@ -146,14 +145,14 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
{
|
||||
getRow.GetRowFromHROW(null, rowHandle, ref Unsafe.AsRef(in IID.IPropertyStore), out var propertyStore);
|
||||
|
||||
if (propertyStore == null)
|
||||
if (propertyStore is null)
|
||||
{
|
||||
Logger.LogError("Failed to get IPropertyStore interface");
|
||||
return false;
|
||||
}
|
||||
|
||||
var searchResult = SearchResult.Create(propertyStore);
|
||||
if (searchResult == null)
|
||||
if (searchResult is null)
|
||||
{
|
||||
Logger.LogError("Failed to create search result");
|
||||
return false;
|
||||
@@ -171,7 +170,7 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
|
||||
public bool FetchRows(int offset, int limit)
|
||||
{
|
||||
if (currentRowset == null)
|
||||
if (currentRowset is null)
|
||||
{
|
||||
Logger.LogError("No rowset to fetch rows from");
|
||||
return false;
|
||||
@@ -241,7 +240,7 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
{
|
||||
var queryStr = QueryStringBuilder.GeneratePrimingQuery();
|
||||
var rowset = ExecuteCommand(queryStr);
|
||||
if (rowset != null)
|
||||
if (rowset is not null)
|
||||
{
|
||||
reuseRowset = rowset;
|
||||
reuseWhereID = GetReuseWhereId(reuseRowset);
|
||||
@@ -261,7 +260,7 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
var guid = typeof(IDBCreateCommand).GUID;
|
||||
session.CreateSession(IntPtr.Zero, ref guid, out var ppDBSession);
|
||||
|
||||
if (ppDBSession == null)
|
||||
if (ppDBSession is null)
|
||||
{
|
||||
Logger.LogError("CreateSession failed");
|
||||
return null;
|
||||
@@ -271,7 +270,7 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
guid = typeof(ICommandText).GUID;
|
||||
createCommand.CreateCommand(IntPtr.Zero, ref guid, out ICommandText commandText);
|
||||
|
||||
if (commandText == null)
|
||||
if (commandText is null)
|
||||
{
|
||||
Logger.LogError("Failed to get ICommandText interface");
|
||||
return null;
|
||||
@@ -342,13 +341,13 @@ internal sealed partial class SearchQuery : IDisposable
|
||||
{
|
||||
var rowsetInfo = (IRowsetInfo)rowset;
|
||||
|
||||
if (rowsetInfo == null)
|
||||
if (rowsetInfo is null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var prop = GetPropset(rowsetInfo);
|
||||
if (prop == null)
|
||||
if (prop is null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ internal sealed class SearchResult
|
||||
ItemUrl = url;
|
||||
IsFolder = isFolder;
|
||||
|
||||
if (LaunchUri == null || LaunchUri.Length == 0)
|
||||
if (LaunchUri is null || LaunchUri.Length == 0)
|
||||
{
|
||||
// Launch the file with the default app, so use the file path
|
||||
LaunchUri = filePath;
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.Marshalling;
|
||||
using ManagedCommon;
|
||||
using ManagedCsWin32;
|
||||
using Microsoft.CmdPal.Ext.Indexer.Indexer.SystemSearch;
|
||||
@@ -28,7 +26,7 @@ internal sealed partial class QueryStringBuilder
|
||||
|
||||
public static string GenerateQuery(string searchText, uint whereId)
|
||||
{
|
||||
if (queryHelper == null)
|
||||
if (queryHelper is null)
|
||||
{
|
||||
ISearchManager searchManager;
|
||||
|
||||
@@ -43,13 +41,13 @@ internal sealed partial class QueryStringBuilder
|
||||
}
|
||||
|
||||
ISearchCatalogManager catalogManager = searchManager.GetCatalog(SystemIndex);
|
||||
if (catalogManager == null)
|
||||
if (catalogManager is null)
|
||||
{
|
||||
throw new ArgumentException($"Failed to get catalog manager for {SystemIndex}");
|
||||
}
|
||||
|
||||
queryHelper = catalogManager.GetQueryHelper();
|
||||
if (queryHelper == null)
|
||||
if (queryHelper is null)
|
||||
{
|
||||
throw new ArgumentException("Failed to get query helper from catalog manager");
|
||||
}
|
||||
|
||||
@@ -44,12 +44,12 @@ internal sealed partial class ActionsListContextItem : CommandContextItem, IDisp
|
||||
{
|
||||
lock (UpdateMoreCommandsLock)
|
||||
{
|
||||
if (actionRuntime == null)
|
||||
if (actionRuntime is null)
|
||||
{
|
||||
actionRuntime = ActionRuntimeManager.InstanceAsync.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
if (actionRuntime == null)
|
||||
if (actionRuntime is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -62,7 +62,7 @@ internal sealed partial class ActionsListContextItem : CommandContextItem, IDisp
|
||||
{
|
||||
var extension = System.IO.Path.GetExtension(fullPath).ToLower(CultureInfo.InvariantCulture);
|
||||
ActionEntity entity = null;
|
||||
if (extension != null)
|
||||
if (extension is not null)
|
||||
{
|
||||
if (extension == ".jpg" || extension == ".jpeg" || extension == ".png")
|
||||
{
|
||||
@@ -74,7 +74,7 @@ internal sealed partial class ActionsListContextItem : CommandContextItem, IDisp
|
||||
}
|
||||
}
|
||||
|
||||
if (entity == null)
|
||||
if (entity is null)
|
||||
{
|
||||
entity = actionRuntime.EntityFactory.CreateFileEntity(fullPath);
|
||||
}
|
||||
@@ -100,7 +100,7 @@ internal sealed partial class ActionsListContextItem : CommandContextItem, IDisp
|
||||
{
|
||||
lock (UpdateMoreCommandsLock)
|
||||
{
|
||||
if (actionRuntime != null)
|
||||
if (actionRuntime is not null)
|
||||
{
|
||||
actionRuntime.ActionCatalog.Changed -= ActionCatalog_Changed;
|
||||
}
|
||||
|
||||
@@ -35,14 +35,14 @@ public sealed partial class DirectoryExplorePage : DynamicListPage
|
||||
|
||||
public override void UpdateSearchText(string oldSearch, string newSearch)
|
||||
{
|
||||
if (_directoryContents == null)
|
||||
if (_directoryContents is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(newSearch))
|
||||
{
|
||||
if (_filteredContents != null)
|
||||
if (_filteredContents is not null)
|
||||
{
|
||||
_filteredContents = null;
|
||||
RaiseItemsChanged(-1);
|
||||
@@ -58,7 +58,7 @@ public sealed partial class DirectoryExplorePage : DynamicListPage
|
||||
newSearch,
|
||||
(s, i) => ListHelpers.ScoreListItem(s, i));
|
||||
|
||||
if (_filteredContents != null)
|
||||
if (_filteredContents is not null)
|
||||
{
|
||||
lock (_filteredContents)
|
||||
{
|
||||
@@ -75,12 +75,12 @@ public sealed partial class DirectoryExplorePage : DynamicListPage
|
||||
|
||||
public override IListItem[] GetItems()
|
||||
{
|
||||
if (_filteredContents != null)
|
||||
if (_filteredContents is not null)
|
||||
{
|
||||
return _filteredContents.ToArray();
|
||||
}
|
||||
|
||||
if (_directoryContents != null)
|
||||
if (_directoryContents is not null)
|
||||
{
|
||||
return _directoryContents.ToArray();
|
||||
}
|
||||
@@ -120,7 +120,7 @@ public sealed partial class DirectoryExplorePage : DynamicListPage
|
||||
try
|
||||
{
|
||||
var stream = ThumbnailHelper.GetThumbnail(item.FilePath).Result;
|
||||
if (stream != null)
|
||||
if (stream is not null)
|
||||
{
|
||||
var data = new IconData(RandomAccessStreamReference.CreateFromStream(stream));
|
||||
icon = new IconInfo(data, data);
|
||||
|
||||
@@ -31,7 +31,7 @@ public sealed partial class DirectoryPage : ListPage
|
||||
|
||||
public override IListItem[] GetItems()
|
||||
{
|
||||
if (_directoryContents != null)
|
||||
if (_directoryContents is not null)
|
||||
{
|
||||
return _directoryContents.ToArray();
|
||||
}
|
||||
@@ -86,7 +86,7 @@ public sealed partial class DirectoryPage : ListPage
|
||||
try
|
||||
{
|
||||
var stream = ThumbnailHelper.GetThumbnail(item.FilePath).Result;
|
||||
if (stream != null)
|
||||
if (stream is not null)
|
||||
{
|
||||
var data = new IconData(RandomAccessStreamReference.CreateFromStream(stream));
|
||||
icon = new IconInfo(data, data);
|
||||
|
||||
@@ -42,7 +42,7 @@ public sealed partial class SearchEngine : IDisposable
|
||||
{
|
||||
hasMore = false;
|
||||
var results = new List<IListItem>();
|
||||
if (_searchQuery != null)
|
||||
if (_searchQuery is not null)
|
||||
{
|
||||
var cookie = _searchQuery.Cookie;
|
||||
if (cookie == queryCookie)
|
||||
@@ -59,7 +59,7 @@ public sealed partial class SearchEngine : IDisposable
|
||||
try
|
||||
{
|
||||
var stream = ThumbnailHelper.GetThumbnail(result.LaunchUri).Result;
|
||||
if (stream != null)
|
||||
if (stream is not null)
|
||||
{
|
||||
var data = new IconData(RandomAccessStreamReference.CreateFromStream(stream));
|
||||
icon = new IconInfo(data, data);
|
||||
|
||||
Reference in New Issue
Block a user