Browse Source

Fix nullable and other warnings.

pull/2639/head
Siegfried Pammer 3 years ago
parent
commit
de5b72114e
  1. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Async.cs
  2. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExceptionHandling.cs
  3. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/PInvoke.cs
  4. 22
      ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs
  5. 18
      ICSharpCode.Decompiler/Util/CollectionExtensions.cs
  6. 4
      ICSharpCode.Decompiler/Util/KeyComparer.cs
  7. 2
      ICSharpCode.Decompiler/Util/LongSet.cs
  8. 8
      ICSharpCode.Decompiler/Util/MultiDictionary.cs
  9. 5
      ICSharpCode.Decompiler/Util/UnionFind.cs
  10. 4
      ILSpy/Analyzers/Builtin/FindTypeInAttributeDecoder.cs
  11. 2
      ILSpy/Languages/CSharpLexer.cs

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Async.cs

@ -193,7 +193,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
catch (Exception ex) catch (Exception ex)
{ {
await Task.Delay(0); await Task.Delay(0);
#pragma warning disable CA2200 // Rethrow to preserve stack details
throw ex; throw ex;
#pragma warning restore CA2200 // Rethrow to preserve stack details
} }
} }

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExceptionHandling.cs

@ -480,7 +480,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
catch (Exception ex) when (ex.Data.IsFixedSize) catch (Exception ex) when (ex.Data.IsFixedSize)
{ {
Console.WriteLine(ex.ToString()); Console.WriteLine(ex.ToString());
#pragma warning disable CA2200 // Rethrow to preserve stack details
throw ex; throw ex;
#pragma warning restore CA2200 // Rethrow to preserve stack details
} }
} }
@ -493,7 +495,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
catch (Exception ex) when (ex is InternalBufferOverflowException) catch (Exception ex) when (ex is InternalBufferOverflowException)
{ {
Console.WriteLine(ex.ToString()); Console.WriteLine(ex.ToString());
#pragma warning disable CA2200 // Rethrow to preserve stack details
throw ex; throw ex;
#pragma warning restore CA2200 // Rethrow to preserve stack details
} }
} }
#endif #endif

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/PInvoke.cs

@ -53,6 +53,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public int bottom; public int bottom;
} }
#pragma warning disable CS0618 // Type or member is obsolete
public static decimal MarshalAttributesOnPropertyAccessors { public static decimal MarshalAttributesOnPropertyAccessors {
[return: MarshalAs(UnmanagedType.Currency)] [return: MarshalAs(UnmanagedType.Currency)]
get { get {
@ -62,6 +63,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
set { set {
} }
} }
#pragma warning restore CS0618 // Type or member is obsolete
[DllImport("xyz.dll", CharSet = CharSet.Auto)] [DllImport("xyz.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]

22
ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs

@ -217,7 +217,9 @@ namespace ICSharpCode.Decompiler.Metadata
public PEFile? ResolveModule(PEFile mainModule, string moduleName) public PEFile? ResolveModule(PEFile mainModule, string moduleName)
{ {
string baseDirectory = Path.GetDirectoryName(mainModule.FileName); string? baseDirectory = Path.GetDirectoryName(mainModule.FileName);
if (baseDirectory == null)
return null;
string moduleFileName = Path.Combine(baseDirectory, moduleName); string moduleFileName = Path.Combine(baseDirectory, moduleName);
return CreatePEFileFromFileName(moduleFileName, ex => new ResolutionException(mainModule.FileName, moduleName, moduleFileName, ex)); return CreatePEFileFromFileName(moduleFileName, ex => new ResolutionException(mainModule.FileName, moduleName, moduleFileName, ex));
} }
@ -381,7 +383,7 @@ namespace ICSharpCode.Decompiler.Metadata
return null; return null;
} }
string FindClosestVersionDirectory(string? basePath, Version? version) string FindClosestVersionDirectory(string basePath, Version? version)
{ {
string? path = null; string? path = null;
foreach (var folder in new DirectoryInfo(basePath).GetDirectories().Select(d => DotNetCorePathFinder.ConvertToVersion(d.Name)) foreach (var folder in new DirectoryInfo(basePath).GetDirectories().Select(d => DotNetCorePathFinder.ConvertToVersion(d.Name))
@ -402,7 +404,7 @@ namespace ICSharpCode.Decompiler.Metadata
if (assembly != null) if (assembly != null)
return assembly; return assembly;
var framework_dir = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName); var framework_dir = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName)!;
var framework_dirs = decompilerRuntime == DecompilerRuntime.Mono var framework_dirs = decompilerRuntime == DecompilerRuntime.Mono
? new[] { framework_dir, Path.Combine(framework_dir, "Facades") } ? new[] { framework_dir, Path.Combine(framework_dir, "Facades") }
: new[] { framework_dir }; : new[] { framework_dir };
@ -444,6 +446,8 @@ namespace ICSharpCode.Decompiler.Metadata
{ {
foreach (var directory in directories) foreach (var directory in directories)
{ {
if (directory == null)
continue;
string? file = SearchDirectory(name, directory); string? file = SearchDirectory(name, directory);
if (file != null) if (file != null)
return file; return file;
@ -457,7 +461,7 @@ namespace ICSharpCode.Decompiler.Metadata
return IsZeroOrAllOnes(reference.Version) || reference.IsRetargetable; return IsZeroOrAllOnes(reference.Version) || reference.IsRetargetable;
} }
string? SearchDirectory(IAssemblyReference name, string? directory) string? SearchDirectory(IAssemblyReference name, string directory)
{ {
var extensions = name.IsWindowsRuntime ? new[] { ".winmd", ".dll" } : new[] { ".dll", ".exe" }; var extensions = name.IsWindowsRuntime ? new[] { ".winmd", ".dll" } : new[] { ".dll", ".exe" };
foreach (var extension in extensions) foreach (var extension in extensions)
@ -581,7 +585,7 @@ namespace ICSharpCode.Decompiler.Metadata
string? GetMonoMscorlibBasePath(Version version) string? GetMonoMscorlibBasePath(Version version)
{ {
var path = Directory.GetParent(typeof(object).Module.FullyQualifiedName).Parent.FullName; var path = Directory.GetParent(typeof(object).Module.FullyQualifiedName)!.Parent!.FullName;
if (version.Major == 1) if (version.Major == 1)
path = Path.Combine(path, "1.0"); path = Path.Combine(path, "1.0");
else if (version.Major == 2) else if (version.Major == 2)
@ -646,7 +650,7 @@ namespace ICSharpCode.Decompiler.Metadata
{ {
return Path.Combine( return Path.Combine(
Directory.GetParent( Directory.GetParent(
Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName)).FullName, Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName)!)!.FullName,
"gac"); "gac");
} }
@ -693,7 +697,7 @@ namespace ICSharpCode.Decompiler.Metadata
return null; return null;
} }
static string GetAssemblyFile(IAssemblyReference reference, string? prefix, string? gac) static string GetAssemblyFile(IAssemblyReference reference, string prefix, string gac)
{ {
var gac_folder = new StringBuilder() var gac_folder = new StringBuilder()
.Append(prefix) .Append(prefix)
@ -719,8 +723,8 @@ namespace ICSharpCode.Decompiler.Metadata
continue; continue;
foreach (var item in new DirectoryInfo(rootPath).EnumerateFiles("*.dll", SearchOption.AllDirectories)) foreach (var item in new DirectoryInfo(rootPath).EnumerateFiles("*.dll", SearchOption.AllDirectories))
{ {
string[] name = Path.GetDirectoryName(item.FullName).Substring(rootPath.Length + 1).Split(new[] { "\\" }, StringSplitOptions.RemoveEmptyEntries); string[]? name = Path.GetDirectoryName(item.FullName)?.Substring(rootPath.Length + 1).Split(new[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
if (name.Length != 2) if (name?.Length != 2)
continue; continue;
var match = Regex.Match(name[1], $"(v4.0_)?(?<version>[^_]+)_(?<culture>[^_]+)?_(?<publicKey>[^_]+)"); var match = Regex.Match(name[1], $"(v4.0_)?(?<version>[^_]+)_(?<culture>[^_]+)?_(?<publicKey>[^_]+)");
if (!match.Success) if (!match.Success)

18
ICSharpCode.Decompiler/Util/CollectionExtensions.cs

@ -15,7 +15,7 @@ namespace ICSharpCode.Decompiler.Util
} }
#if !NETCORE #if !NETCORE
public static IEnumerable<(A, B)> Zip<A, B>(this IEnumerable<A>? input1, IEnumerable<B>? input2) public static IEnumerable<(A, B)> Zip<A, B>(this IEnumerable<A> input1, IEnumerable<B> input2)
{ {
return input1.Zip(input2, (a, b) => (a, b)); return input1.Zip(input2, (a, b) => (a, b));
} }
@ -61,7 +61,7 @@ namespace ICSharpCode.Decompiler.Util
} }
#if !NETCORE #if !NETCORE
public static HashSet<T> ToHashSet<T>(this IEnumerable<T>? input) public static HashSet<T> ToHashSet<T>(this IEnumerable<T> input)
{ {
return new HashSet<T>(input); return new HashSet<T>(input);
} }
@ -354,7 +354,7 @@ namespace ICSharpCode.Decompiler.Util
list.RemoveAt(list.Count - 1); list.RemoveAt(list.Count - 1);
} }
public static T? OnlyOrDefault<T>(this IEnumerable<T>? source, Func<T, bool>? predicate) => OnlyOrDefault(source.Where(predicate)); public static T? OnlyOrDefault<T>(this IEnumerable<T> source, Func<T, bool> predicate) => OnlyOrDefault(source.Where(predicate));
public static T? OnlyOrDefault<T>(this IEnumerable<T> source) public static T? OnlyOrDefault<T>(this IEnumerable<T> source)
{ {
@ -373,14 +373,14 @@ namespace ICSharpCode.Decompiler.Util
#region Aliases/shortcuts for Enumerable extension methods #region Aliases/shortcuts for Enumerable extension methods
public static bool Any<T>(this ICollection<T> list) => list.Count > 0; public static bool Any<T>(this ICollection<T> list) => list.Count > 0;
public static bool Any<T>(this T[]? array, Predicate<T>? match) => Array.Exists(array, match); public static bool Any<T>(this T[] array, Predicate<T> match) => Array.Exists(array, match);
public static bool Any<T>(this List<T> list, Predicate<T>? match) => list.Exists(match); public static bool Any<T>(this List<T> list, Predicate<T> match) => list.Exists(match);
public static bool All<T>(this T[]? array, Predicate<T>? match) => Array.TrueForAll(array, match); public static bool All<T>(this T[] array, Predicate<T> match) => Array.TrueForAll(array, match);
public static bool All<T>(this List<T> list, Predicate<T>? match) => list.TrueForAll(match); public static bool All<T>(this List<T> list, Predicate<T> match) => list.TrueForAll(match);
public static T FirstOrDefault<T>(this T[]? array, Predicate<T>? predicate) => Array.Find(array, predicate); public static T? FirstOrDefault<T>(this T[] array, Predicate<T> predicate) => Array.Find(array, predicate);
public static T FirstOrDefault<T>(this List<T> list, Predicate<T>? predicate) => list.Find(predicate); public static T? FirstOrDefault<T>(this List<T> list, Predicate<T> predicate) => list.Find(predicate);
public static T Last<T>(this IList<T> list) => list[list.Count - 1]; public static T Last<T>(this IList<T> list) => list[list.Count - 1];
#endregion #endregion

4
ICSharpCode.Decompiler/Util/KeyComparer.cs

@ -69,12 +69,12 @@ namespace ICSharpCode.Decompiler.Util
this.keyEqualityComparer = keyEqualityComparer; this.keyEqualityComparer = keyEqualityComparer;
} }
public int Compare(TElement x, TElement y) public int Compare(TElement? x, TElement? y)
{ {
return keyComparer.Compare(keySelector(x), keySelector(y)); return keyComparer.Compare(keySelector(x), keySelector(y));
} }
public bool Equals(TElement x, TElement y) public bool Equals(TElement? x, TElement? y)
{ {
return keyEqualityComparer.Equals(keySelector(x), keySelector(y)); return keyEqualityComparer.Equals(keySelector(x), keySelector(y));
} }

2
ICSharpCode.Decompiler/Util/LongSet.cs

@ -83,7 +83,7 @@ namespace ICSharpCode.Decompiler.Util
/// <summary> /// <summary>
/// Creates a new LongSet the contains the values from the specified intervals. /// Creates a new LongSet the contains the values from the specified intervals.
/// </summary> /// </summary>
public LongSet(IEnumerable<LongInterval>? intervals) public LongSet(IEnumerable<LongInterval> intervals)
: this(MergeOverlapping(intervals.Where(i => !i.IsEmpty).OrderBy(i => i.Start)).ToImmutableArray()) : this(MergeOverlapping(intervals.Where(i => !i.IsEmpty).OrderBy(i => i.Start)).ToImmutableArray())
{ {
} }

8
ICSharpCode.Decompiler/Util/MultiDictionary.cs

@ -25,7 +25,7 @@ namespace ICSharpCode.Decompiler.Util
/// <summary> /// <summary>
/// A dictionary that allows multiple pairs with the same key. /// A dictionary that allows multiple pairs with the same key.
/// </summary> /// </summary>
public class MultiDictionary<TKey, TValue> : ILookup<TKey, TValue> public class MultiDictionary<TKey, TValue> : ILookup<TKey, TValue> where TKey : notnull
{ {
readonly Dictionary<TKey, List<TValue>> dict; readonly Dictionary<TKey, List<TValue>> dict;
@ -41,8 +41,7 @@ namespace ICSharpCode.Decompiler.Util
public void Add(TKey key, TValue value) public void Add(TKey key, TValue value)
{ {
List<TValue> valueList; if (!dict.TryGetValue(key, out List<TValue>? valueList))
if (!dict.TryGetValue(key, out valueList))
{ {
valueList = new List<TValue>(); valueList = new List<TValue>();
dict.Add(key, valueList); dict.Add(key, valueList);
@ -52,8 +51,7 @@ namespace ICSharpCode.Decompiler.Util
public bool Remove(TKey key, TValue value) public bool Remove(TKey key, TValue value)
{ {
List<TValue> valueList; if (dict.TryGetValue(key, out List<TValue>? valueList))
if (dict.TryGetValue(key, out valueList))
{ {
if (valueList.Remove(value)) if (valueList.Remove(value))
{ {

5
ICSharpCode.Decompiler/Util/UnionFind.cs

@ -24,7 +24,7 @@ namespace ICSharpCode.Decompiler.Util
/// <summary> /// <summary>
/// Union-Find data structure. /// Union-Find data structure.
/// </summary> /// </summary>
public class UnionFind<T> public class UnionFind<T> where T : notnull
{ {
Dictionary<T, Node> mapping; Dictionary<T, Node> mapping;
@ -48,8 +48,7 @@ namespace ICSharpCode.Decompiler.Util
Node GetNode(T element) Node GetNode(T element)
{ {
Node node; if (!mapping.TryGetValue(element, out Node? node))
if (!mapping.TryGetValue(element, out node))
{ {
node = new Node(element); node = new Node(element);
node.parent = node; node.parent = node;

4
ILSpy/Analyzers/Builtin/FindTypeInAttributeDecoder.cs

@ -65,7 +65,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
public FindTypeInAttributeDecoder(MetadataModule currentModule, ITypeDefinition type) public FindTypeInAttributeDecoder(MetadataModule currentModule, ITypeDefinition type)
{ {
this.currentModule = currentModule; this.currentModule = currentModule;
this.declaringModule = type.ParentModule.PEFile ?? throw new InvalidOperationException("Cannot use MetadataModule without PEFile as context."); this.declaringModule = type.ParentModule?.PEFile ?? throw new InvalidOperationException("Cannot use MetadataModule without PEFile as context.");
this.handle = (TypeDefinitionHandle)type.MetadataToken; this.handle = (TypeDefinitionHandle)type.MetadataToken;
this.primitiveType = type.KnownTypeCode == KnownTypeCode.None ? 0 : type.KnownTypeCode.ToPrimitiveTypeCode(); this.primitiveType = type.KnownTypeCode == KnownTypeCode.None ? 0 : type.KnownTypeCode.ToPrimitiveTypeCode();
} }
@ -138,7 +138,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
{ {
result = TokenSearchResult.SystemType; result = TokenSearchResult.SystemType;
} }
if (td.MetadataToken == this.handle && td.ParentModule.PEFile == declaringModule) if (td.MetadataToken == this.handle && td.ParentModule?.PEFile == declaringModule)
{ {
result |= TokenSearchResult.Found; result |= TokenSearchResult.Found;
} }

2
ILSpy/Languages/CSharpLexer.cs

@ -107,7 +107,7 @@ namespace ICSharpCode.ILSpy
} }
} }
internal abstract class AbstractLexer internal abstract class AbstractLexer : IDisposable
{ {
LATextReader reader; LATextReader reader;
int col = 1; int col = 1;

Loading…
Cancel
Save