From 7ebfc5ded58f40efa886afe311c9f3993300c5ce Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 13 Feb 2021 14:31:39 +0100 Subject: [PATCH] Fix #2294: Issues discovered by static analysis. --- .../CSharp/Resolver/CSharpConversions.cs | 1 - .../Util/ResXResourceWriter.cs | 1 - ILSpy.AddIn/Commands/OpenILSpyCommand.cs | 16 +++++++++------- ILSpy.BamlDecompiler/BamlResourceEntryNode.cs | 9 ++++----- .../Xaml/XamlPathDeserializer.cs | 2 +- .../Builtin/MethodVirtualUsedByAnalyzer.cs | 4 +--- ILSpy/AvalonEdit/TextMarkerService.cs | 6 ++---- ILSpy/Images/Images.cs | 2 +- ILSpy/MainWindow.xaml.cs | 3 +-- ILSpy/Metadata/FlagsFilterControl.xaml.cs | 7 ++++--- SharpTreeView/SharpTreeNodeCollection.cs | 3 +-- SharpTreeView/TreeFlattener.cs | 3 +-- 12 files changed, 25 insertions(+), 32 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/Resolver/CSharpConversions.cs b/ICSharpCode.Decompiler/CSharp/Resolver/CSharpConversions.cs index fd099d0e2..b88829c78 100644 --- a/ICSharpCode.Decompiler/CSharp/Resolver/CSharpConversions.cs +++ b/ICSharpCode.Decompiler/CSharp/Resolver/CSharpConversions.cs @@ -826,7 +826,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver case TypeCode.UInt16: return val >= UInt16.MinValue && val <= UInt16.MaxValue; case TypeCode.UInt32: - return val >= 0; case TypeCode.UInt64: return val >= 0; } diff --git a/ICSharpCode.Decompiler/Util/ResXResourceWriter.cs b/ICSharpCode.Decompiler/Util/ResXResourceWriter.cs index 58a1d3339..fd86f9791 100644 --- a/ICSharpCode.Decompiler/Util/ResXResourceWriter.cs +++ b/ICSharpCode.Decompiler/Util/ResXResourceWriter.cs @@ -60,7 +60,6 @@ namespace ICSharpCode.Decompiler.Util public static readonly string ByteArraySerializedObjectMimeType = "application/x-microsoft.net.object.bytearray.base64"; public static readonly string DefaultSerializedObjectMimeType = BinSerializedObjectMimeType; public static readonly string ResMimeType = "text/microsoft-resx"; - public static readonly string ResourceSchema = schema; public static readonly string SoapSerializedObjectMimeType = "application/x-microsoft.net.object.soap.base64"; public static readonly string Version = "2.0"; #endregion // Static Fields diff --git a/ILSpy.AddIn/Commands/OpenILSpyCommand.cs b/ILSpy.AddIn/Commands/OpenILSpyCommand.cs index 07555c92c..6759a54c1 100644 --- a/ILSpy.AddIn/Commands/OpenILSpyCommand.cs +++ b/ILSpy.AddIn/Commands/OpenILSpyCommand.cs @@ -1,11 +1,8 @@ using System; using System.Collections.Generic; using System.ComponentModel.Design; -using System.Diagnostics; using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; using Microsoft.VisualStudio.Shell; @@ -93,10 +90,15 @@ namespace ICSharpCode.ILSpy.AddIn.Commands foreach (var projectReference in parentProject.ProjectReferences) { var roslynProject = owner.Workspace.CurrentSolution.GetProject(projectReference.ProjectId); - var project = FindProject(owner.DTE.Solution.Projects.OfType(), roslynProject.FilePath); - if (roslynProject != null && project != null) - dict.Add(roslynProject.AssemblyName, - new DetectedReference(roslynProject.AssemblyName, Utils.GetProjectOutputAssembly(project, roslynProject), true)); + if (roslynProject != null) + { + var project = FindProject(owner.DTE.Solution.Projects.OfType(), roslynProject.FilePath); + if (project != null) + { + dict.Add(roslynProject.AssemblyName, + new DetectedReference(roslynProject.AssemblyName, Utils.GetProjectOutputAssembly(project, roslynProject), true)); + } + } } return dict; } diff --git a/ILSpy.BamlDecompiler/BamlResourceEntryNode.cs b/ILSpy.BamlDecompiler/BamlResourceEntryNode.cs index f1d6dafee..bbb4ddbc8 100644 --- a/ILSpy.BamlDecompiler/BamlResourceEntryNode.cs +++ b/ILSpy.BamlDecompiler/BamlResourceEntryNode.cs @@ -57,8 +57,8 @@ namespace ILSpy.BamlDecompiler AvalonEditTextOutput output = new AvalonEditTextOutput(); try { - if (LoadBaml(output, token)) - highlighting = HighlightingManager.Instance.GetDefinitionByExtension(".xml"); + LoadBaml(output, token); + highlighting = HighlightingManager.Instance.GetDefinitionByExtension(".xml"); } catch (Exception ex) { @@ -71,13 +71,12 @@ namespace ILSpy.BamlDecompiler return true; } - bool LoadBaml(AvalonEditTextOutput output, CancellationToken cancellationToken) + void LoadBaml(AvalonEditTextOutput output, CancellationToken cancellationToken) { - var asm = this.Ancestors().OfType().FirstOrDefault().LoadedAssembly; + var asm = this.Ancestors().OfType().First().LoadedAssembly; using var data = OpenStream(); XDocument xamlDocument = LoadIntoDocument(asm.GetPEFileOrNull(), asm.GetAssemblyResolver(), data, cancellationToken); output.Write(xamlDocument.ToString()); - return true; } internal static XDocument LoadIntoDocument(PEFile module, IAssemblyResolver assemblyResolver, diff --git a/ILSpy.BamlDecompiler/Xaml/XamlPathDeserializer.cs b/ILSpy.BamlDecompiler/Xaml/XamlPathDeserializer.cs index 4c2b20172..c23a4e77f 100644 --- a/ILSpy.BamlDecompiler/Xaml/XamlPathDeserializer.cs +++ b/ILSpy.BamlDecompiler/Xaml/XamlPathDeserializer.cs @@ -174,7 +174,7 @@ namespace ILSpy.BamlDecompiler.Xaml var size = ReadPoint(reader); double angle = reader.ReadXamlDouble(); - sb.AppendFormat(CultureInfo.InvariantCulture, "A{0} {2:R} {2} {3} {4}", + sb.AppendFormat(CultureInfo.InvariantCulture, "A{0} {1:R} {2} {3} {4}", size, angle, largeArc ? '1' : '0', sweepDirection ? '1' : '0', pt1); break; } diff --git a/ILSpy/Analyzers/Builtin/MethodVirtualUsedByAnalyzer.cs b/ILSpy/Analyzers/Builtin/MethodVirtualUsedByAnalyzer.cs index a6e63ecdc..b7e807639 100644 --- a/ILSpy/Analyzers/Builtin/MethodVirtualUsedByAnalyzer.cs +++ b/ILSpy/Analyzers/Builtin/MethodVirtualUsedByAnalyzer.cs @@ -17,12 +17,10 @@ // DEALINGS IN THE SOFTWARE. using System.Collections.Generic; -using System.ComponentModel.Composition; using System.Diagnostics; using System.Linq; using System.Reflection.Metadata; -using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.Disassembler; using ICSharpCode.Decompiler.Metadata; using ICSharpCode.Decompiler.TypeSystem; @@ -134,7 +132,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin case HandleKind.MethodSpecification: case HandleKind.MemberReference: var m = (mainModule.ResolveEntity(member, genericContext) as IMember)?.MemberDefinition; - if (m.MetadataToken == analyzedMethod.MetadataToken && m.ParentModule.PEFile == analyzedMethod.ParentModule.PEFile) + if (m != null && m.MetadataToken == analyzedMethod.MetadataToken && m.ParentModule.PEFile == analyzedMethod.ParentModule.PEFile) { return true; } diff --git a/ILSpy/AvalonEdit/TextMarkerService.cs b/ILSpy/AvalonEdit/TextMarkerService.cs index a6bd3ef6c..9a1d1598c 100644 --- a/ILSpy/AvalonEdit/TextMarkerService.cs +++ b/ILSpy/AvalonEdit/TextMarkerService.cs @@ -116,8 +116,7 @@ namespace ICSharpCode.ILSpy.AvalonEdit internal void Redraw(ISegment segment) { textView.Redraw(segment, DispatcherPriority.Normal); - if (RedrawRequested != null) - RedrawRequested(this, EventArgs.Empty); + RedrawRequested?.Invoke(this, EventArgs.Empty); } public event EventHandler RedrawRequested; @@ -280,8 +279,7 @@ namespace ICSharpCode.ILSpy.AvalonEdit internal void OnDeleted() { - if (Deleted != null) - Deleted(this, EventArgs.Empty); + Deleted?.Invoke(this, EventArgs.Empty); } void Redraw() diff --git a/ILSpy/Images/Images.cs b/ILSpy/Images/Images.cs index 29a2c6847..b0cc50f7b 100644 --- a/ILSpy/Images/Images.cs +++ b/ILSpy/Images/Images.cs @@ -251,7 +251,7 @@ namespace ICSharpCode.ILSpy baseImage = Images.Literal; break; case MemberIcon.EnumValue: - baseImage = Images.Literal; + baseImage = Images.EnumValue; break; case MemberIcon.Property: baseImage = Images.Property; diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 545ee6bcd..96951570a 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -783,8 +783,7 @@ namespace ICSharpCode.ILSpy nd => nd.AncestorsAndSelf().OfType().Any( a => oldAssemblies.Contains(a.LoadedAssembly)))); } - if (CurrentAssemblyListChanged != null) - CurrentAssemblyListChanged(this, e); + CurrentAssemblyListChanged?.Invoke(this, e); } void LoadInitialAssemblies() diff --git a/ILSpy/Metadata/FlagsFilterControl.xaml.cs b/ILSpy/Metadata/FlagsFilterControl.xaml.cs index 18ab15620..7a8225a69 100644 --- a/ILSpy/Metadata/FlagsFilterControl.xaml.cs +++ b/ILSpy/Metadata/FlagsFilterControl.xaml.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Reflection; using System.Windows; using System.Windows.Controls; @@ -43,7 +41,10 @@ namespace ICSharpCode.ILSpy.Metadata base.OnApplyTemplate(); listBox = Template.FindName("ListBox", this) as ListBox; - listBox.ItemsSource = FlagGroup.GetFlags(FlagsType, neutralItem: ""); + if (listBox != null) + { + listBox.ItemsSource = FlagGroup.GetFlags(FlagsType, neutralItem: ""); + } var filter = Filter; diff --git a/SharpTreeView/SharpTreeNodeCollection.cs b/SharpTreeView/SharpTreeNodeCollection.cs index 3d65737b8..f21d46686 100644 --- a/SharpTreeView/SharpTreeNodeCollection.cs +++ b/SharpTreeView/SharpTreeNodeCollection.cs @@ -47,8 +47,7 @@ namespace ICSharpCode.TreeView try { parent.OnChildrenChanged(e); - if (CollectionChanged != null) - CollectionChanged(this, e); + CollectionChanged?.Invoke(this, e); } finally { diff --git a/SharpTreeView/TreeFlattener.cs b/SharpTreeView/TreeFlattener.cs index eb23fbcfb..39175f470 100644 --- a/SharpTreeView/TreeFlattener.cs +++ b/SharpTreeView/TreeFlattener.cs @@ -47,8 +47,7 @@ namespace ICSharpCode.TreeView public void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e) { - if (CollectionChanged != null) - CollectionChanged(this, e); + CollectionChanged?.Invoke(this, e); } public void NodesInserted(int index, IEnumerable nodes)