From 3b6a650cf1144212a82b2f2319bae38e4b03af96 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Thu, 3 Nov 2022 21:01:55 +0100 Subject: [PATCH] Fix nullability warnings in ILSpyX --- ICSharpCode.ILSpyX/ICSharpCode.ILSpyX.csproj | 1 + .../Search/AbstractEntitySearchStrategy.cs | 7 +++---- .../Search/AssemblySearchStrategy.cs | 20 +++++++++++++------ ICSharpCode.ILSpyX/Search/CSharpLexer.cs | 2 ++ .../Search/LiteralSearchStrategy.cs | 11 +++++++--- ICSharpCode.ILSpyX/Search/SearchResult.cs | 12 +++++++++++ 6 files changed, 40 insertions(+), 13 deletions(-) diff --git a/ICSharpCode.ILSpyX/ICSharpCode.ILSpyX.csproj b/ICSharpCode.ILSpyX/ICSharpCode.ILSpyX.csproj index a1490aeb3..f5f01af1f 100644 --- a/ICSharpCode.ILSpyX/ICSharpCode.ILSpyX.csproj +++ b/ICSharpCode.ILSpyX/ICSharpCode.ILSpyX.csproj @@ -4,6 +4,7 @@ net6.0 enable true + nullable True ..\ICSharpCode.Decompiler\ICSharpCode.Decompiler.snk diff --git a/ICSharpCode.ILSpyX/Search/AbstractEntitySearchStrategy.cs b/ICSharpCode.ILSpyX/Search/AbstractEntitySearchStrategy.cs index a64a58b19..60eb05baf 100644 --- a/ICSharpCode.ILSpyX/Search/AbstractEntitySearchStrategy.cs +++ b/ICSharpCode.ILSpyX/Search/AbstractEntitySearchStrategy.cs @@ -37,12 +37,12 @@ namespace ICSharpCode.ILSpyX.Search this.apiVisibility = apiVisibility; } - protected bool CheckVisibility(IEntity entity) + protected bool CheckVisibility(IEntity? entity) { if (apiVisibility == ApiVisibility.All) return true; - do + while (entity != null) { if (apiVisibility == ApiVisibility.PublicOnly) { @@ -58,7 +58,6 @@ namespace ICSharpCode.ILSpyX.Search } entity = entity.DeclaringTypeDefinition; } - while (entity != null); return true; } @@ -67,7 +66,7 @@ namespace ICSharpCode.ILSpyX.Search { if (searchRequest.InAssembly != null) { - if (!entity.ParentModule.FullAssemblyName.Contains(searchRequest.InAssembly)) + if (entity.ParentModule == null || !entity.ParentModule.FullAssemblyName.Contains(searchRequest.InAssembly)) { return false; } diff --git a/ICSharpCode.ILSpyX/Search/AssemblySearchStrategy.cs b/ICSharpCode.ILSpyX/Search/AssemblySearchStrategy.cs index 5f32db836..3d0b1fb87 100644 --- a/ICSharpCode.ILSpyX/Search/AssemblySearchStrategy.cs +++ b/ICSharpCode.ILSpyX/Search/AssemblySearchStrategy.cs @@ -41,19 +41,27 @@ namespace ICSharpCode.ILSpyX.Search if (searchKind == AssemblySearchKind.NameOrFileName) { - string localName = GetNameToMatch(module, AssemblySearchKind.Name); - string fileName = Path.GetFileName(GetNameToMatch(module, AssemblySearchKind.FilePath)); - if (IsMatch(localName) || IsMatch(fileName)) + string? localName = GetNameToMatch(module, AssemblySearchKind.Name); + string? filePath = GetNameToMatch(module, AssemblySearchKind.FilePath); + if (localName != null && IsMatch(localName)) + { OnFoundResult(module); + } + else if (filePath != null) + { + string fileName = Path.GetFileName(filePath); + if (IsMatch(fileName)) + OnFoundResult(module); + } return; } - string name = GetNameToMatch(module, searchKind); - if (IsMatch(name)) + string? name = GetNameToMatch(module, searchKind); + if (name != null && IsMatch(name)) OnFoundResult(module); } - string GetNameToMatch(PEFile module, AssemblySearchKind kind) + string? GetNameToMatch(PEFile module, AssemblySearchKind kind) { switch (kind) { diff --git a/ICSharpCode.ILSpyX/Search/CSharpLexer.cs b/ICSharpCode.ILSpyX/Search/CSharpLexer.cs index ab689b95a..05a6edb97 100644 --- a/ICSharpCode.ILSpyX/Search/CSharpLexer.cs +++ b/ICSharpCode.ILSpyX/Search/CSharpLexer.cs @@ -16,6 +16,8 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable disable + using System; using System.Collections.Generic; using System.Globalization; diff --git a/ICSharpCode.ILSpyX/Search/LiteralSearchStrategy.cs b/ICSharpCode.ILSpyX/Search/LiteralSearchStrategy.cs index 767024d1e..a5b813820 100644 --- a/ICSharpCode.ILSpyX/Search/LiteralSearchStrategy.cs +++ b/ICSharpCode.ILSpyX/Search/LiteralSearchStrategy.cs @@ -17,6 +17,7 @@ // DEALINGS IN THE SOFTWARE. using System; using System.Collections.Concurrent; +using System.Diagnostics; using System.Reflection.Metadata; using System.Threading; @@ -35,8 +36,8 @@ namespace ICSharpCode.ILSpyX.Search { public class LiteralSearchStrategy : AbstractEntitySearchStrategy { - readonly TypeCode searchTermLiteralType; - readonly object searchTermLiteralValue; + readonly TypeCode searchTermLiteralType = TypeCode.Empty; + readonly object? searchTermLiteralValue; public LiteralSearchStrategy(ILanguage language, ApiVisibility apiVisibility, SearchRequest request, IProducerConsumerCollection resultQueue) @@ -131,10 +132,12 @@ namespace ICSharpCode.ILSpyX.Search case TypeCode.Single: case TypeCode.Double: case TypeCode.String: + Debug.Assert(searchTermLiteralValue != null); return searchTermLiteralValue.Equals(val); default: // substring search with searchTerm - return IsMatch(val.ToString()); + string? valAsString = val.ToString(); + return valAsString != null && IsMatch(valAsString); } } @@ -143,6 +146,7 @@ namespace ICSharpCode.ILSpyX.Search var blob = module.Reader.GetMethodBody(methodDefinition.RelativeVirtualAddress).GetILReader(); if (searchTermLiteralType == TypeCode.Int64) { + Debug.Assert(searchTermLiteralValue != null); long val = (long)searchTermLiteralValue; while (blob.RemainingBytes > 0) { @@ -209,6 +213,7 @@ namespace ICSharpCode.ILSpyX.Search } else if (searchTermLiteralType != TypeCode.Empty) { + Debug.Assert(searchTermLiteralValue != null); ILOpCode expectedCode; switch (searchTermLiteralType) { diff --git a/ICSharpCode.ILSpyX/Search/SearchResult.cs b/ICSharpCode.ILSpyX/Search/SearchResult.cs index d0e94bf40..abe18ae0d 100644 --- a/ICSharpCode.ILSpyX/Search/SearchResult.cs +++ b/ICSharpCode.ILSpyX/Search/SearchResult.cs @@ -42,14 +42,18 @@ namespace ICSharpCode.ILSpyX.Search public float Fitness { get; set; } +#nullable disable public string Name { get; set; } public string Location { get; set; } public string Assembly { get; set; } +#nullable enable public object? ToolTip { get; set; } +#nullable disable public object Image { get; set; } public object LocationImage { get; set; } public object AssemblyImage { get; set; } +#nullable enable public override string ToString() { @@ -76,25 +80,33 @@ namespace ICSharpCode.ILSpyX.Search public class MemberSearchResult : SearchResult { +#nullable disable public IEntity Member { get; set; } public override object Reference => Member; +#nullable enable } public class ResourceSearchResult : SearchResult { +#nullable disable public Resource Resource { get; set; } +#nullable enable public override object Reference => ValueTuple.Create(Resource, Name); } public class AssemblySearchResult : SearchResult { +#nullable disable public PEFile Module { get; set; } public override object Reference => Module; +#nullable enable } public class NamespaceSearchResult : SearchResult { +#nullable disable public INamespace Namespace { get; set; } public override object Reference => Namespace; +#nullable enable } } \ No newline at end of file