diff --git a/ICSharpCode.Decompiler/Util/BitSet.cs b/ICSharpCode.Decompiler/Util/BitSet.cs index 9ca0a2b0c..5d7ecf429 100644 --- a/ICSharpCode.Decompiler/Util/BitSet.cs +++ b/ICSharpCode.Decompiler/Util/BitSet.cs @@ -15,6 +15,7 @@ // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable using System; using System.Diagnostics; diff --git a/ICSharpCode.Decompiler/Util/BusyManager.cs b/ICSharpCode.Decompiler/Util/BusyManager.cs index f277fc40a..abe2aeba4 100644 --- a/ICSharpCode.Decompiler/Util/BusyManager.cs +++ b/ICSharpCode.Decompiler/Util/BusyManager.cs @@ -15,6 +15,7 @@ // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable using System; using System.Collections.Generic; @@ -36,9 +37,9 @@ namespace ICSharpCode.Decompiler.Util { public static readonly BusyLock Failed = new BusyLock(null); - readonly List objectList; + readonly List? objectList; - internal BusyLock(List objectList) + internal BusyLock(List? objectList) { this.objectList = objectList; } @@ -56,13 +57,13 @@ namespace ICSharpCode.Decompiler.Util } } - [ThreadStatic] static List _activeObjects; + [ThreadStatic] static List? _activeObjects; - public static BusyLock Enter(object obj) + public static BusyLock Enter(object? obj) { - List activeObjects = _activeObjects; + List? activeObjects = _activeObjects; if (activeObjects == null) - activeObjects = _activeObjects = new List(); + activeObjects = _activeObjects = new List(); for (int i = 0; i < activeObjects.Count; i++) { if (activeObjects[i] == obj) diff --git a/ICSharpCode.Decompiler/Util/CSharpPrimitiveCast.cs b/ICSharpCode.Decompiler/Util/CSharpPrimitiveCast.cs index b57516b45..cfc8c854c 100644 --- a/ICSharpCode.Decompiler/Util/CSharpPrimitiveCast.cs +++ b/ICSharpCode.Decompiler/Util/CSharpPrimitiveCast.cs @@ -16,7 +16,10 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable + using System; +using System.Diagnostics.CodeAnalysis; namespace ICSharpCode.Decompiler.Util { @@ -34,7 +37,8 @@ namespace ICSharpCode.Decompiler.Util /// /// Overflow checking is enabled and an overflow occurred. /// The cast is invalid, e.g. casting a boolean to an integer. - public static object Cast(TypeCode targetType, object input, bool checkForOverflow) + [return: NotNullIfNotNull("input")] + public static object? Cast(TypeCode targetType, object? input, bool checkForOverflow) { if (input == null) return null; diff --git a/ICSharpCode.Decompiler/Util/CacheManager.cs b/ICSharpCode.Decompiler/Util/CacheManager.cs index 75bef2203..2c3bae375 100644 --- a/ICSharpCode.Decompiler/Util/CacheManager.cs +++ b/ICSharpCode.Decompiler/Util/CacheManager.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 enable + using System; using System.Collections.Concurrent; diff --git a/ICSharpCode.Decompiler/Util/CallbackOnDispose.cs b/ICSharpCode.Decompiler/Util/CallbackOnDispose.cs index ac83ff562..cb02b5321 100644 --- a/ICSharpCode.Decompiler/Util/CallbackOnDispose.cs +++ b/ICSharpCode.Decompiler/Util/CallbackOnDispose.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 enable + using System; using System.Threading; @@ -30,7 +32,7 @@ namespace ICSharpCode.Decompiler.Util /// public sealed class CallbackOnDispose : IDisposable { - Action action; + Action? action; public CallbackOnDispose(Action action) { @@ -41,7 +43,7 @@ namespace ICSharpCode.Decompiler.Util public void Dispose() { - Action a = Interlocked.Exchange(ref action, null); + Action? a = Interlocked.Exchange(ref action, null); if (a != null) { a(); diff --git a/ICSharpCode.Decompiler/Util/CollectionExtensions.cs b/ICSharpCode.Decompiler/Util/CollectionExtensions.cs index 2b54131a9..6bc2132fd 100644 --- a/ICSharpCode.Decompiler/Util/CollectionExtensions.cs +++ b/ICSharpCode.Decompiler/Util/CollectionExtensions.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; @@ -14,13 +15,13 @@ namespace ICSharpCode.Decompiler.Util } #if !NETCORE - public static IEnumerable<(A, B)> Zip(this IEnumerable input1, IEnumerable input2) + public static IEnumerable<(A, B)> Zip(this IEnumerable? input1, IEnumerable? input2) { return input1.Zip(input2, (a, b) => (a, b)); } #endif - public static IEnumerable<(A, B)> ZipLongest(this IEnumerable input1, IEnumerable input2) + public static IEnumerable<(A?, B?)> ZipLongest(this IEnumerable input1, IEnumerable input2) { using (var it1 = input1.GetEnumerator()) { @@ -60,7 +61,7 @@ namespace ICSharpCode.Decompiler.Util } #if !NETCORE - public static HashSet ToHashSet(this IEnumerable input) + public static HashSet ToHashSet(this IEnumerable? input) { return new HashSet(input); } @@ -76,14 +77,14 @@ namespace ICSharpCode.Decompiler.Util return input.Skip(input.Count - count); } - public static T PopOrDefault(this Stack stack) + public static T? PopOrDefault(this Stack stack) { if (stack.Count == 0) return default(T); return stack.Pop(); } - public static T PeekOrDefault(this Stack stack) + public static T? PeekOrDefault(this Stack stack) { if (stack.Count == 0) return default(T); @@ -277,7 +278,7 @@ namespace ICSharpCode.Decompiler.Util /// Returns the minimum element. /// /// The input sequence is empty - public static T MinBy(this IEnumerable source, Func keySelector, IComparer keyComparer) + public static T MinBy(this IEnumerable source, Func keySelector, IComparer? keyComparer) { if (source == null) throw new ArgumentNullException(nameof(source)); @@ -318,7 +319,7 @@ namespace ICSharpCode.Decompiler.Util /// Returns the maximum element. /// /// The input sequence is empty - public static T MaxBy(this IEnumerable source, Func keySelector, IComparer keyComparer) + public static T MaxBy(this IEnumerable source, Func keySelector, IComparer? keyComparer) { if (source == null) throw new ArgumentNullException(nameof(source)); @@ -353,12 +354,12 @@ namespace ICSharpCode.Decompiler.Util list.RemoveAt(list.Count - 1); } - public static T OnlyOrDefault(this IEnumerable source, Func predicate) => OnlyOrDefault(source.Where(predicate)); + public static T? OnlyOrDefault(this IEnumerable? source, Func? predicate) => OnlyOrDefault(source.Where(predicate)); - public static T OnlyOrDefault(this IEnumerable source) + public static T? OnlyOrDefault(this IEnumerable source) { bool any = false; - T first = default; + T? first = default; foreach (var t in source) { if (any) @@ -372,14 +373,14 @@ namespace ICSharpCode.Decompiler.Util #region Aliases/shortcuts for Enumerable extension methods public static bool Any(this ICollection list) => list.Count > 0; - public static bool Any(this T[] array, Predicate match) => Array.Exists(array, match); - public static bool Any(this List list, Predicate match) => list.Exists(match); + public static bool Any(this T[]? array, Predicate? match) => Array.Exists(array, match); + public static bool Any(this List list, Predicate? match) => list.Exists(match); - public static bool All(this T[] array, Predicate match) => Array.TrueForAll(array, match); - public static bool All(this List list, Predicate match) => list.TrueForAll(match); + public static bool All(this T[]? array, Predicate? match) => Array.TrueForAll(array, match); + public static bool All(this List list, Predicate? match) => list.TrueForAll(match); - public static T FirstOrDefault(this T[] array, Predicate predicate) => Array.Find(array, predicate); - public static T FirstOrDefault(this List list, Predicate predicate) => list.Find(predicate); + public static T FirstOrDefault(this T[]? array, Predicate? predicate) => Array.Find(array, predicate); + public static T FirstOrDefault(this List list, Predicate? predicate) => list.Find(predicate); public static T Last(this IList list) => list[list.Count - 1]; #endregion diff --git a/ICSharpCode.Decompiler/Util/EmptyList.cs b/ICSharpCode.Decompiler/Util/EmptyList.cs index 2b4e6c45d..f02871910 100644 --- a/ICSharpCode.Decompiler/Util/EmptyList.cs +++ b/ICSharpCode.Decompiler/Util/EmptyList.cs @@ -15,6 +15,7 @@ // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable using System; using System.Collections; @@ -91,11 +92,11 @@ namespace ICSharpCode.Decompiler.Util } T IEnumerator.Current { - get { return default(T); } + get { throw new NotSupportedException(); } } object IEnumerator.Current { - get { return default(T); } + get { throw new NotSupportedException(); } } void IDisposable.Dispose() diff --git a/ICSharpCode.Decompiler/Util/ExtensionMethods.cs b/ICSharpCode.Decompiler/Util/ExtensionMethods.cs index 57c97199a..bdb070712 100644 --- a/ICSharpCode.Decompiler/Util/ExtensionMethods.cs +++ b/ICSharpCode.Decompiler/Util/ExtensionMethods.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team +#nullable enable +// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -26,7 +27,7 @@ namespace ICSharpCode.Decompiler.Util /// static class ExtensionMethods { - public static Predicate And(this Predicate filter1, Predicate filter2) + public static Predicate? And(this Predicate? filter1, Predicate? filter2) { if (filter1 == null) return filter2; diff --git a/ICSharpCode.Decompiler/Util/FileUtility.cs b/ICSharpCode.Decompiler/Util/FileUtility.cs index aaf26fa06..d2d93923f 100644 --- a/ICSharpCode.Decompiler/Util/FileUtility.cs +++ b/ICSharpCode.Decompiler/Util/FileUtility.cs @@ -16,7 +16,10 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable + using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; @@ -28,9 +31,10 @@ namespace ICSharpCode.Decompiler.Util /// Gets the normalized version of fileName. /// Slashes are replaced with backslashes, backreferences "." and ".." are 'evaluated'. /// - public static string NormalizePath(string fileName) + [return: NotNullIfNotNull("fileName")] + public static string? NormalizePath(string? fileName) { - if (string.IsNullOrEmpty(fileName)) + if (fileName == null || fileName.Length == 0) return fileName; int i; @@ -169,14 +173,14 @@ namespace ICSharpCode.Decompiler.Util && (fileName[1] == '\\' || fileName[1] == '/'); } - public static bool IsEqualFileName(string fileName1, string fileName2) + public static bool IsEqualFileName(string? fileName1, string? fileName2) { return string.Equals(NormalizePath(fileName1), NormalizePath(fileName2), StringComparison.OrdinalIgnoreCase); } - public static bool IsBaseDirectory(string baseDirectory, string testDirectory) + public static bool IsBaseDirectory(string? baseDirectory, string? testDirectory) { if (baseDirectory == null || testDirectory == null) return false; @@ -189,9 +193,10 @@ namespace ICSharpCode.Decompiler.Util return testDirectory.StartsWith(baseDirectory, StringComparison.OrdinalIgnoreCase); } - static string AddTrailingSeparator(string input) + [return: NotNullIfNotNull("input")] + static string? AddTrailingSeparator(string? input) { - if (string.IsNullOrEmpty(input)) + if (input == null || input.Length == 0) return input; if (input[input.Length - 1] == Path.DirectorySeparatorChar || input[input.Length - 1] == Path.AltDirectorySeparatorChar) return input; @@ -212,9 +217,9 @@ namespace ICSharpCode.Decompiler.Util /// Converts a given absolute path and a given base path to a path that leads /// from the base path to the absoulte path. (as a relative path) /// - public static string GetRelativePath(string baseDirectoryPath, string absPath) + public static string GetRelativePath(string? baseDirectoryPath, string absPath) { - if (string.IsNullOrEmpty(baseDirectoryPath)) + if (baseDirectoryPath == null || baseDirectoryPath.Length == 0) { return absPath; } @@ -252,7 +257,8 @@ namespace ICSharpCode.Decompiler.Util return erg.ToString(); } - public static string TrimPath(string path, int max_chars) + [return: NotNullIfNotNull("path")] + public static string? TrimPath(string? path, int max_chars) { const char ellipsis = '\u2026'; // HORIZONTAL ELLIPSIS const int ellipsisLength = 2; diff --git a/ICSharpCode.Decompiler/Util/GraphVizGraph.cs b/ICSharpCode.Decompiler/Util/GraphVizGraph.cs index bfa574005..92069e7d9 100644 --- a/ICSharpCode.Decompiler/Util/GraphVizGraph.cs +++ b/ICSharpCode.Decompiler/Util/GraphVizGraph.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 enable + using System; using System.Collections.Generic; using System.Diagnostics; @@ -34,8 +36,8 @@ namespace ICSharpCode.Decompiler.Util List nodes = new List(); List edges = new List(); - public string rankdir; - public string Title; + public string? rankdir; + public string? Title; public void AddEdge(GraphVizEdge edge) { @@ -58,7 +60,7 @@ namespace ICSharpCode.Decompiler.Util Show(null); } - public void Show(string name) + public void Show(string? name) { if (name == null) name = Title; @@ -83,7 +85,7 @@ namespace ICSharpCode.Decompiler.Util } } - static void WriteGraphAttribute(TextWriter writer, string name, string value) + static void WriteGraphAttribute(TextWriter writer, string name, string? value) { if (value != null) writer.WriteLine("{0}={1};", name, Escape(value)); @@ -105,7 +107,7 @@ namespace ICSharpCode.Decompiler.Util } } - internal static void WriteAttribute(TextWriter writer, string name, string value, ref bool isFirst) + internal static void WriteAttribute(TextWriter writer, string name, string? value, ref bool isFirst) { if (value != null) { @@ -141,13 +143,13 @@ namespace ICSharpCode.Decompiler.Util public readonly string Source, Target; /// edge stroke color - public string color; + public string? color; /// use edge to affect node ranking public bool? constraint; - public string label; + public string? label; - public string style; + public string? style; /// point size of label public int? fontsize; @@ -184,9 +186,9 @@ namespace ICSharpCode.Decompiler.Util sealed class GraphVizNode { public readonly string ID; - public string label; + public string? label; - public string labelloc; + public string? labelloc; /// point size of label public int? fontsize; @@ -195,10 +197,10 @@ namespace ICSharpCode.Decompiler.Util public double? height; /// space around label - public string margin; + public string? margin; /// node shape - public string shape; + public string? shape; public GraphVizNode(string id) { diff --git a/ICSharpCode.Decompiler/Util/Interval.cs b/ICSharpCode.Decompiler/Util/Interval.cs index 7dd74f64a..7a50e2b1e 100644 --- a/ICSharpCode.Decompiler/Util/Interval.cs +++ b/ICSharpCode.Decompiler/Util/Interval.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2016 Daniel Grunwald +#nullable enable +// Copyright (c) 2016 Daniel Grunwald // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -111,7 +112,7 @@ namespace ICSharpCode.Decompiler.Util } #region Equals and GetHashCode implementation - public override bool Equals(object obj) + public override bool Equals(object? obj) { return (obj is Interval) && Equals((Interval)obj); } @@ -281,7 +282,7 @@ namespace ICSharpCode.Decompiler.Util } #region Equals and GetHashCode implementation - public override bool Equals(object obj) + public override bool Equals(object? obj) { return (obj is LongInterval) && Equals((LongInterval)obj); } diff --git a/ICSharpCode.Decompiler/Util/KeyComparer.cs b/ICSharpCode.Decompiler/Util/KeyComparer.cs index 7cd6d7047..b1412d09b 100644 --- a/ICSharpCode.Decompiler/Util/KeyComparer.cs +++ b/ICSharpCode.Decompiler/Util/KeyComparer.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team +#nullable enable +// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software diff --git a/ICSharpCode.Decompiler/Util/LongSet.cs b/ICSharpCode.Decompiler/Util/LongSet.cs index 697c43330..ac23cc67c 100644 --- a/ICSharpCode.Decompiler/Util/LongSet.cs +++ b/ICSharpCode.Decompiler/Util/LongSet.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2016 Daniel Grunwald +#nullable enable +// Copyright (c) 2016 Daniel Grunwald // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -82,7 +83,7 @@ namespace ICSharpCode.Decompiler.Util /// /// Creates a new LongSet the contains the values from the specified intervals. /// - public LongSet(IEnumerable intervals) + public LongSet(IEnumerable? intervals) : this(MergeOverlapping(intervals.Where(i => !i.IsEmpty).OrderBy(i => i.Start)).ToImmutableArray()) { } @@ -350,7 +351,7 @@ namespace ICSharpCode.Decompiler.Util } #region Equals and GetHashCode implementation - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is LongSet && SetEquals((LongSet)obj); } diff --git a/ICSharpCode.Decompiler/Util/MultiDictionary.cs b/ICSharpCode.Decompiler/Util/MultiDictionary.cs index ec93a866c..3bdfd989b 100644 --- a/ICSharpCode.Decompiler/Util/MultiDictionary.cs +++ b/ICSharpCode.Decompiler/Util/MultiDictionary.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team +#nullable enable +// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -33,7 +34,7 @@ namespace ICSharpCode.Decompiler.Util dict = new Dictionary>(); } - public MultiDictionary(IEqualityComparer comparer) + public MultiDictionary(IEqualityComparer? comparer) { dict = new Dictionary>(comparer); } diff --git a/ICSharpCode.Decompiler/Util/Platform.cs b/ICSharpCode.Decompiler/Util/Platform.cs index b91a91060..69419877c 100644 --- a/ICSharpCode.Decompiler/Util/Platform.cs +++ b/ICSharpCode.Decompiler/Util/Platform.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team +#nullable enable +// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software diff --git a/ICSharpCode.Decompiler/Util/ProjectedList.cs b/ICSharpCode.Decompiler/Util/ProjectedList.cs index fa632005e..5026f2f1c 100644 --- a/ICSharpCode.Decompiler/Util/ProjectedList.cs +++ b/ICSharpCode.Decompiler/Util/ProjectedList.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team +#nullable enable +// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -25,7 +26,7 @@ namespace ICSharpCode.Decompiler.Util { readonly IList input; readonly Func projection; - readonly TOutput[] items; + readonly TOutput?[] items; public ProjectedList(IList input, Func projection) { @@ -35,12 +36,12 @@ namespace ICSharpCode.Decompiler.Util throw new ArgumentNullException(nameof(projection)); this.input = input; this.projection = projection; - this.items = new TOutput[input.Count]; + this.items = new TOutput?[input.Count]; } public TOutput this[int index] { get { - TOutput output = LazyInit.VolatileRead(ref items[index]); + TOutput? output = LazyInit.VolatileRead(ref items[index]); if (output != null) { return output; @@ -72,7 +73,7 @@ namespace ICSharpCode.Decompiler.Util readonly IList input; readonly TContext context; readonly Func projection; - readonly TOutput[] items; + readonly TOutput?[] items; public ProjectedList(TContext context, IList input, Func projection) { @@ -83,12 +84,12 @@ namespace ICSharpCode.Decompiler.Util this.input = input; this.context = context; this.projection = projection; - this.items = new TOutput[input.Count]; + this.items = new TOutput?[input.Count]; } public TOutput this[int index] { get { - TOutput output = LazyInit.VolatileRead(ref items[index]); + TOutput? output = LazyInit.VolatileRead(ref items[index]); if (output != null) { return output; diff --git a/ICSharpCode.Decompiler/Util/ReferenceComparer.cs b/ICSharpCode.Decompiler/Util/ReferenceComparer.cs index cb4485bce..43b1e3afe 100644 --- a/ICSharpCode.Decompiler/Util/ReferenceComparer.cs +++ b/ICSharpCode.Decompiler/Util/ReferenceComparer.cs @@ -15,22 +15,23 @@ // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable using System.Collections.Generic; using System.Runtime.CompilerServices; namespace ICSharpCode.Decompiler.Util { - public sealed class ReferenceComparer : IEqualityComparer + public sealed class ReferenceComparer : IEqualityComparer { public readonly static ReferenceComparer Instance = new ReferenceComparer(); - public new bool Equals(object x, object y) + public new bool Equals(object? x, object? y) { return x == y; } - public int GetHashCode(object obj) + public int GetHashCode(object? obj) { return RuntimeHelpers.GetHashCode(obj); } diff --git a/ICSharpCode.Decompiler/Util/ResourcesFile.cs b/ICSharpCode.Decompiler/Util/ResourcesFile.cs index a55637006..988fa01a2 100644 --- a/ICSharpCode.Decompiler/Util/ResourcesFile.cs +++ b/ICSharpCode.Decompiler/Util/ResourcesFile.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2018 Daniel Grunwald +#nullable enable +// Copyright (c) 2018 Daniel Grunwald // Based on the .NET Core ResourceReader; make available under the MIT license // by the .NET Foundation. // @@ -30,7 +31,7 @@ namespace ICSharpCode.Decompiler.Util /// /// .resources file. /// - public class ResourcesFile : IEnumerable>, IDisposable + public class ResourcesFile : IEnumerable>, IDisposable { sealed class MyBinaryReader : BinaryReader { @@ -87,7 +88,7 @@ namespace ICSharpCode.Decompiler.Util readonly long fileStartPosition; readonly long nameSectionPosition; readonly long dataSectionPosition; - long[] startPositions; + long[]? startPositions; /// /// Creates a new ResourcesFile. @@ -285,7 +286,7 @@ namespace ICSharpCode.Decompiler.Util return true; } - object LoadObject(int dataOffset) + object? LoadObject(int dataOffset) { try { @@ -318,7 +319,7 @@ namespace ICSharpCode.Decompiler.Util // from that location. // Anyone who calls LoadObject should make sure they take a lock so // no one can cause us to do a seek in here. - private object LoadObjectV1(int dataOffset) + private object? LoadObjectV1(int dataOffset) { Debug.Assert(System.Threading.Monitor.IsEntered(reader)); reader.Seek(dataSectionPosition + dataOffset, SeekOrigin.Begin); @@ -372,7 +373,7 @@ namespace ICSharpCode.Decompiler.Util } } - private object LoadObjectV2(int dataOffset) + private object? LoadObjectV2(int dataOffset) { Debug.Assert(System.Threading.Monitor.IsEntered(reader)); reader.Seek(dataSectionPosition + dataOffset, SeekOrigin.Begin); @@ -464,19 +465,19 @@ namespace ICSharpCode.Decompiler.Util } } - public object GetResourceValue(int index) + public object? GetResourceValue(int index) { GetResourceName(index, out int dataOffset); return LoadObject(dataOffset); } - public IEnumerator> GetEnumerator() + public IEnumerator> GetEnumerator() { for (int i = 0; i < numResources; i++) { string name = GetResourceName(i, out int dataOffset); - object val = LoadObject(dataOffset); - yield return new KeyValuePair(name, val); + object? val = LoadObject(dataOffset); + yield return new KeyValuePair(name, val); } } @@ -487,7 +488,7 @@ namespace ICSharpCode.Decompiler.Util long[] GetStartPositions() { - long[] positions = LazyInit.VolatileRead(ref startPositions); + long[]? positions = LazyInit.VolatileRead(ref startPositions); if (positions != null) return positions; lock (reader) @@ -541,11 +542,11 @@ namespace ICSharpCode.Decompiler.Util public class ResourceSerializedObject { - public string TypeName { get; } + public string? TypeName { get; } readonly ResourcesFile file; readonly long position; - internal ResourceSerializedObject(string typeName, ResourcesFile file, long position) + internal ResourceSerializedObject(string? typeName, ResourcesFile file, long position) { this.TypeName = typeName; this.file = file; diff --git a/ICSharpCode.Decompiler/Util/TreeTraversal.cs b/ICSharpCode.Decompiler/Util/TreeTraversal.cs index a12b35c46..f750ac137 100644 --- a/ICSharpCode.Decompiler/Util/TreeTraversal.cs +++ b/ICSharpCode.Decompiler/Util/TreeTraversal.cs @@ -1,4 +1,5 @@ -// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team +#nullable enable +// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software @@ -32,7 +33,7 @@ namespace ICSharpCode.Decompiler.Util /// The root element of the tree. /// The function that gets the children of an element. /// Iterator that enumerates the tree structure in pre-order. - public static IEnumerable PreOrder(T root, Func> recursion) + public static IEnumerable PreOrder(T root, Func?> recursion) { return PreOrder(new T[] { root }, recursion); } @@ -43,7 +44,7 @@ namespace ICSharpCode.Decompiler.Util /// The root elements of the forest. /// The function that gets the children of an element. /// Iterator that enumerates the tree structure in pre-order. - public static IEnumerable PreOrder(IEnumerable input, Func> recursion) + public static IEnumerable PreOrder(IEnumerable input, Func?> recursion) { Stack> stack = new Stack>(); try @@ -55,7 +56,7 @@ namespace ICSharpCode.Decompiler.Util { T element = stack.Peek().Current; yield return element; - IEnumerable children = recursion(element); + IEnumerable? children = recursion(element); if (children != null) { stack.Push(children.GetEnumerator()); @@ -79,7 +80,7 @@ namespace ICSharpCode.Decompiler.Util /// The root element of the tree. /// The function that gets the children of an element. /// Iterator that enumerates the tree structure in post-order. - public static IEnumerable PostOrder(T root, Func> recursion) + public static IEnumerable PostOrder(T root, Func?> recursion) { return PostOrder(new T[] { root }, recursion); } @@ -90,7 +91,7 @@ namespace ICSharpCode.Decompiler.Util /// The root elements of the forest. /// The function that gets the children of an element. /// Iterator that enumerates the tree structure in post-order. - public static IEnumerable PostOrder(IEnumerable input, Func> recursion) + public static IEnumerable PostOrder(IEnumerable input, Func?> recursion) { Stack> stack = new Stack>(); try @@ -101,7 +102,7 @@ namespace ICSharpCode.Decompiler.Util while (stack.Peek().MoveNext()) { T element = stack.Peek().Current; - IEnumerable children = recursion(element); + IEnumerable? children = recursion(element); if (children != null) { stack.Push(children.GetEnumerator()); diff --git a/ICSharpCode.Decompiler/Util/UnicodeNewline.cs b/ICSharpCode.Decompiler/Util/UnicodeNewline.cs index dca342ce0..a81824529 100644 --- a/ICSharpCode.Decompiler/Util/UnicodeNewline.cs +++ b/ICSharpCode.Decompiler/Util/UnicodeNewline.cs @@ -1,3 +1,4 @@ +#nullable enable // // UnicodeNewline.cs // @@ -118,7 +119,7 @@ namespace ICSharpCode.Decompiler.Util /// 0 == no new line, otherwise it returns either 1 or 2 depending of the length of the delimiter. /// The current character. /// A callback getting the next character (may be null). - public static int GetDelimiterLength(char curChar, Func nextChar = null) + public static int GetDelimiterLength(char curChar, Func? nextChar = null) { if (curChar == CR) { @@ -161,7 +162,7 @@ namespace ICSharpCode.Decompiler.Util /// The length of the delimiter /// The type of the delimiter /// A callback getting the next character (may be null). - public static bool TryGetDelimiterLengthAndType(char curChar, out int length, out UnicodeNewline type, Func nextChar = null) + public static bool TryGetDelimiterLengthAndType(char curChar, out int length, out UnicodeNewline type, Func? nextChar = null) { if (curChar == CR) { @@ -275,7 +276,7 @@ namespace ICSharpCode.Decompiler.Util /// 0 == no new line, otherwise it returns either 1 or 2 depending of the length of the delimiter. /// The current character. /// A callback getting the next character (may be null). - public static UnicodeNewline GetDelimiterType(char curChar, Func nextChar = null) + public static UnicodeNewline GetDelimiterType(char curChar, Func? nextChar = null) { switch (curChar) { diff --git a/ICSharpCode.Decompiler/Util/UnionFind.cs b/ICSharpCode.Decompiler/Util/UnionFind.cs index 4bc0acbdf..15c06f2d7 100644 --- a/ICSharpCode.Decompiler/Util/UnionFind.cs +++ b/ICSharpCode.Decompiler/Util/UnionFind.cs @@ -15,6 +15,7 @@ // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable using System.Collections.Generic; @@ -32,6 +33,12 @@ namespace ICSharpCode.Decompiler.Util public int rank; public Node parent; public T value; + + internal Node(T value) + { + this.value = value; + this.parent = this; + } } public UnionFind() @@ -44,10 +51,7 @@ namespace ICSharpCode.Decompiler.Util Node node; if (!mapping.TryGetValue(element, out node)) { - node = new Node { - value = element, - rank = 0 - }; + node = new Node(element); node.parent = node; mapping.Add(element, node); } @@ -84,5 +88,3 @@ namespace ICSharpCode.Decompiler.Util } } } - - diff --git a/ICSharpCode.Decompiler/Util/Win32Resources.cs b/ICSharpCode.Decompiler/Util/Win32Resources.cs index a5adf81f4..c3574fc9b 100644 --- a/ICSharpCode.Decompiler/Util/Win32Resources.cs +++ b/ICSharpCode.Decompiler/Util/Win32Resources.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection.PortableExecutable; @@ -15,21 +16,21 @@ namespace ICSharpCode.Decompiler.Util /// /// /// - public static unsafe Win32ResourceDirectory ReadWin32Resources(this PEReader pe) + public static unsafe Win32ResourceDirectory? ReadWin32Resources(this PEReader pe) { if (pe == null) { throw new ArgumentNullException(nameof(pe)); } - int rva = pe.PEHeaders.PEHeader.ResourceTableDirectory.RelativeVirtualAddress; + int rva = pe.PEHeaders.PEHeader?.ResourceTableDirectory.RelativeVirtualAddress ?? 0; if (rva == 0) return null; byte* pRoot = pe.GetSectionData(rva).Pointer; return new Win32ResourceDirectory(pe, pRoot, 0, new Win32ResourceName("Root")); } - public static Win32ResourceDirectory Find(this Win32ResourceDirectory root, Win32ResourceName type) + public static Win32ResourceDirectory? Find(this Win32ResourceDirectory root, Win32ResourceName type) { if (root is null) throw new ArgumentNullException(nameof(root)); @@ -41,7 +42,7 @@ namespace ICSharpCode.Decompiler.Util return root.FindDirectory(type); } - public static Win32ResourceDirectory Find(this Win32ResourceDirectory root, Win32ResourceName type, Win32ResourceName name) + public static Win32ResourceDirectory? Find(this Win32ResourceDirectory root, Win32ResourceName type, Win32ResourceName name) { if (root is null) throw new ArgumentNullException(nameof(root)); @@ -55,7 +56,7 @@ namespace ICSharpCode.Decompiler.Util return root.FindDirectory(type)?.FindDirectory(name); } - public static Win32ResourceData Find(this Win32ResourceDirectory root, Win32ResourceName type, Win32ResourceName name, Win32ResourceName langId) + public static Win32ResourceData? Find(this Win32ResourceDirectory root, Win32ResourceName type, Win32ResourceName name, Win32ResourceName langId) { if (root is null) throw new ArgumentNullException(nameof(root)); @@ -122,7 +123,7 @@ namespace ICSharpCode.Decompiler.Util return new string(pString->NameString, 0, pString->Length); } - public Win32ResourceDirectory FindDirectory(Win32ResourceName name) + public Win32ResourceDirectory? FindDirectory(Win32ResourceName name) { foreach (var directory in Directories) { @@ -132,7 +133,7 @@ namespace ICSharpCode.Decompiler.Util return null; } - public Win32ResourceData FindData(Win32ResourceName name) + public Win32ResourceData? FindData(Win32ResourceName name) { foreach (var data in Datas) { @@ -142,12 +143,12 @@ namespace ICSharpCode.Decompiler.Util return null; } - public Win32ResourceDirectory FirstDirectory() + public Win32ResourceDirectory? FirstDirectory() { return Directories.Count != 0 ? Directories[0] : null; } - public Win32ResourceData FirstData() + public Win32ResourceData? FirstData() { return Datas.Count != 0 ? Datas[0] : null; } @@ -248,7 +249,7 @@ namespace ICSharpCode.Decompiler.Util return _name.GetHashCode(); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (!(obj is Win32ResourceName name)) return false; diff --git a/ILSpy.BamlDecompiler.Tests/ILSpy.BamlDecompiler.Tests.csproj b/ILSpy.BamlDecompiler.Tests/ILSpy.BamlDecompiler.Tests.csproj index 85c0519e3..1b2337cb3 100644 --- a/ILSpy.BamlDecompiler.Tests/ILSpy.BamlDecompiler.Tests.csproj +++ b/ILSpy.BamlDecompiler.Tests/ILSpy.BamlDecompiler.Tests.csproj @@ -1,5 +1,5 @@  - + net472 @@ -112,4 +112,4 @@ - \ No newline at end of file +