From cba0eada695563465f24b82ab705e0eaf7a859cf Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 30 Jun 2020 12:57:57 +1000 Subject: [PATCH] More nullability --- .../Extensions/CollectionExtensions.cs | 9 +++-- src/BizHawk.Common/Extensions/IOExtensions.cs | 5 +-- src/BizHawk.Common/OSTailoredCode.cs | 8 ++--- src/BizHawk.Common/SettingsUtil.cs | 33 +++++++++++-------- src/BizHawk.Common/UndoHistory.cs | 4 +-- src/BizHawk.Common/Win32/MotWHack.cs | 2 -- .../Win32/ProcessorFeatureImports.cs | 2 -- src/BizHawk.Version/VersionInfo.cs | 4 +-- 8 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/BizHawk.Common/Extensions/CollectionExtensions.cs b/src/BizHawk.Common/Extensions/CollectionExtensions.cs index 7b2ae2d63f..52514bc60b 100644 --- a/src/BizHawk.Common/Extensions/CollectionExtensions.cs +++ b/src/BizHawk.Common/Extensions/CollectionExtensions.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System; +using System; using System.Collections.Generic; using System.Linq; @@ -16,7 +14,8 @@ namespace BizHawk.Common.CollectionExtensions return desc ? source.OrderByDescending(keySelector) : source.OrderBy(keySelector); } - public static int LowerBoundBinarySearch(this IList list, Func keySelector, TKey key) where TKey : IComparable + public static int LowerBoundBinarySearch(this IList list, Func keySelector, TKey key) + where TKey : IComparable { int min = 0; int max = list.Count; @@ -73,7 +72,7 @@ namespace BizHawk.Common.CollectionExtensions /// not found after mapping over /// implementation from https://stackoverflow.com/a/1766369/7467292 public static T BinarySearch(this IList list, Func keySelector, TKey key) - where TKey : IComparable + where TKey : IComparable { int min = 0; int max = list.Count; diff --git a/src/BizHawk.Common/Extensions/IOExtensions.cs b/src/BizHawk.Common/Extensions/IOExtensions.cs index abdef1be60..7c52de6917 100644 --- a/src/BizHawk.Common/Extensions/IOExtensions.cs +++ b/src/BizHawk.Common/Extensions/IOExtensions.cs @@ -1,7 +1,4 @@ -#nullable disable - -using System; -using System.IO; +using System.IO; using System.Text; namespace BizHawk.Common.IOExtensions diff --git a/src/BizHawk.Common/OSTailoredCode.cs b/src/BizHawk.Common/OSTailoredCode.cs index cb21efbb15..c3b14dd498 100644 --- a/src/BizHawk.Common/OSTailoredCode.cs +++ b/src/BizHawk.Common/OSTailoredCode.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics; using System.Runtime.InteropServices; @@ -21,7 +19,7 @@ namespace BizHawk.Common private static readonly Lazy _LinkedLibManager = new Lazy(() => CurrentOS switch { - DistinctOS.Linux => (ILinkedLibManager) new UnixMonoLLManager(), + DistinctOS.Linux => new UnixMonoLLManager(), DistinctOS.macOS => new UnixMonoLLManager(), DistinctOS.Windows => new WindowsLLManager(), _ => throw new ArgumentOutOfRangeException() @@ -130,8 +128,8 @@ namespace BizHawk.Common /// stdout is discarded if false /// stderr is discarded if false /// OS is implicit and needs to be checked at callsite. Returned has not been started. - public static Process ConstructSubshell(string cmd, string args, bool checkStdout = true, bool checkStderr = false) => - new Process { + public static Process ConstructSubshell(string cmd, string args, bool checkStdout = true, bool checkStderr = false) + => new Process { StartInfo = new ProcessStartInfo { Arguments = args, CreateNoWindow = true, diff --git a/src/BizHawk.Common/SettingsUtil.cs b/src/BizHawk.Common/SettingsUtil.cs index bf6a979fe0..a5eda4401d 100644 --- a/src/BizHawk.Common/SettingsUtil.cs +++ b/src/BizHawk.Common/SettingsUtil.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System; +using System; using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; @@ -13,18 +11,26 @@ namespace BizHawk.Common { private sealed class DefaultValueSetter { - public Action SetDefaultValues; - public object[] DefaultValues; + public readonly Action SetDefaultValues; + + public readonly object[] DefaultValues; + + public DefaultValueSetter(Action setDefaultValues, object[] defaultValues) + { + SetDefaultValues = setDefaultValues; + DefaultValues = defaultValues; + } } - private static IDictionary DefaultValueSetters = new ConcurrentDictionary(); + private static readonly IDictionary DefaultValueSetters = new ConcurrentDictionary(); /// /// set all properties (not fields!) of obj with a DefaultValueAttribute to that value /// /// the obj to act on public static void SetDefaultValues(T obj) + where T : notnull { if (!DefaultValueSetters.TryGetValue(typeof(T), out var f)) { @@ -34,7 +40,7 @@ namespace BizHawk.Common f.SetDefaultValues(obj, f.DefaultValues); } - private static Dictionary IntTypes = new Dictionary + private static readonly Dictionary IntTypes = new Dictionary { { typeof(byte), OpCodes.Conv_U1 }, { typeof(sbyte), OpCodes.Conv_I1 }, @@ -64,9 +70,9 @@ namespace BizHawk.Common MethodInfo method = prop.GetSetMethod(true); foreach (object attr in prop.GetCustomAttributes(true)) { - if (attr is DefaultValueAttribute) + if (attr is DefaultValueAttribute dvAttr) { - object value = (attr as DefaultValueAttribute).Value; + var value = dvAttr.Value; Type desiredType = method.GetParameters()[0].ParameterType; Type sourceType = value.GetType(); @@ -98,11 +104,10 @@ namespace BizHawk.Common } il.Emit(OpCodes.Pop); il.Emit(OpCodes.Ret); - return new DefaultValueSetter - { - SetDefaultValues = (Action)dyn.CreateDelegate(typeof(Action)), - DefaultValues = DefaultValues.ToArray() - }; + return new DefaultValueSetter( + (Action) dyn.CreateDelegate(typeof(Action)), + DefaultValues.ToArray() + ); } } } diff --git a/src/BizHawk.Common/UndoHistory.cs b/src/BizHawk.Common/UndoHistory.cs index 35da8f56a0..66bedf1354 100644 --- a/src/BizHawk.Common/UndoHistory.cs +++ b/src/BizHawk.Common/UndoHistory.cs @@ -1,6 +1,4 @@ -#nullable disable - -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; namespace BizHawk.Common diff --git a/src/BizHawk.Common/Win32/MotWHack.cs b/src/BizHawk.Common/Win32/MotWHack.cs index 12b2a6d8d6..0a122cc438 100644 --- a/src/BizHawk.Common/Win32/MotWHack.cs +++ b/src/BizHawk.Common/Win32/MotWHack.cs @@ -1,5 +1,3 @@ -#nullable disable - namespace BizHawk.Common { /// This code (and an import for ) is duplicated in each executable project because it needs to be used before loading assemblies. diff --git a/src/BizHawk.Common/Win32/ProcessorFeatureImports.cs b/src/BizHawk.Common/Win32/ProcessorFeatureImports.cs index 02d3f01037..e6082d006d 100644 --- a/src/BizHawk.Common/Win32/ProcessorFeatureImports.cs +++ b/src/BizHawk.Common/Win32/ProcessorFeatureImports.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Runtime.InteropServices; namespace BizHawk.Common diff --git a/src/BizHawk.Version/VersionInfo.cs b/src/BizHawk.Version/VersionInfo.cs index c4709bb232..e5fdecae8c 100644 --- a/src/BizHawk.Version/VersionInfo.cs +++ b/src/BizHawk.Version/VersionInfo.cs @@ -1,4 +1,4 @@ -#nullable disable +#nullable enable using System.IO; using System.Reflection; @@ -14,7 +14,7 @@ internal static class VersionInfo public const string HomePage = "http://tasvideos.org/BizHawk.html"; public static readonly bool DeveloperBuild = true; - public static readonly string CustomBuildString; + public static readonly string? CustomBuildString; public static string GetEmuVersion() {