From 29c15adbbf1a1c2219c3201e87a1d1fb30b0b6bf Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 10 Sep 2024 04:31:25 +1000 Subject: [PATCH] Promote BHI1007 to a build error, fixing some cases and muting others "Don't use target-typed new for throw expressions" where there was a type name checked-in before being removed, I've restored it (they were all `Exception`) --- .global.editorconfig.ini | 2 +- src/BizHawk.Bizware.Audio/SDL2WavStream.cs | 2 ++ src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs | 2 ++ src/BizHawk.Bizware.Graphics/OpenGL/SDL2OpenGLContext.cs | 2 ++ .../Renderers/SDLImGui2DRenderer.cs | 2 ++ src/BizHawk.Bizware.Input/KeyInput/X11KeyInput.cs | 5 ++++- src/BizHawk.Bizware.Input/SDL2/SDL2InputAdapter.cs | 2 ++ src/BizHawk.Client.Common/XmlGame.cs | 4 ++-- src/BizHawk.Client.EmuHawk/AVOut/AviWriter.cs | 6 +++--- .../RetroAchievements/RCheevos.Debug.cs | 2 ++ src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs | 2 ++ .../RetroAchievements/RetroAchievements.GameVerification.cs | 2 ++ src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs | 2 +- src/BizHawk.Common/Extensions/PathExtensions.cs | 4 +++- src/BizHawk.Common/OSTailoredCode.cs | 2 +- src/BizHawk.Emulation.Common/Sound/SDLResampler.cs | 2 ++ src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs | 2 +- src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs | 2 ++ .../Consoles/Nintendo/NDS/MelonDS.cs | 2 +- src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.cs | 2 +- 20 files changed, 38 insertions(+), 13 deletions(-) diff --git a/.global.editorconfig.ini b/.global.editorconfig.ini index c10d5cdcd1..d8ad54eacf 100644 --- a/.global.editorconfig.ini +++ b/.global.editorconfig.ini @@ -18,7 +18,7 @@ dotnet_diagnostic.BHI1005.severity = error # Do not discard local variables dotnet_diagnostic.BHI1006.severity = error # Don't use target-typed new for throw expressions -dotnet_diagnostic.BHI1007.severity = suggestion +dotnet_diagnostic.BHI1007.severity = error # Don't call this.GetType() in sealed type, use typeof operator dotnet_diagnostic.BHI1100.severity = error # Don't call this.GetType(), use typeof operator (or replace subtype check with better encapsulation) diff --git a/src/BizHawk.Bizware.Audio/SDL2WavStream.cs b/src/BizHawk.Bizware.Audio/SDL2WavStream.cs index 859f0bf78d..9b4da0ddc8 100644 --- a/src/BizHawk.Bizware.Audio/SDL2WavStream.cs +++ b/src/BizHawk.Bizware.Audio/SDL2WavStream.cs @@ -7,6 +7,8 @@ using static SDL2.SDL; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Bizware.Audio { internal sealed class SDL2WavStream : Stream, ISpanStream diff --git a/src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs b/src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs index cff3942fed..85c18dccd5 100644 --- a/src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs +++ b/src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs @@ -3,6 +3,8 @@ using Silk.NET.OpenGL; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Bizware.Graphics { /// diff --git a/src/BizHawk.Bizware.Graphics/OpenGL/SDL2OpenGLContext.cs b/src/BizHawk.Bizware.Graphics/OpenGL/SDL2OpenGLContext.cs index fe29fe3924..8ec638b74d 100644 --- a/src/BizHawk.Bizware.Graphics/OpenGL/SDL2OpenGLContext.cs +++ b/src/BizHawk.Bizware.Graphics/OpenGL/SDL2OpenGLContext.cs @@ -9,6 +9,8 @@ using static SDL2.SDL; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Bizware.Graphics { /// diff --git a/src/BizHawk.Bizware.Graphics/Renderers/SDLImGui2DRenderer.cs b/src/BizHawk.Bizware.Graphics/Renderers/SDLImGui2DRenderer.cs index cc91ed9883..1c80caff58 100644 --- a/src/BizHawk.Bizware.Graphics/Renderers/SDLImGui2DRenderer.cs +++ b/src/BizHawk.Bizware.Graphics/Renderers/SDLImGui2DRenderer.cs @@ -7,6 +7,8 @@ using static SDL2.SDL; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Bizware.Graphics { /// diff --git a/src/BizHawk.Bizware.Input/KeyInput/X11KeyInput.cs b/src/BizHawk.Bizware.Input/KeyInput/X11KeyInput.cs index f744bfdc1d..a57f33f1e9 100644 --- a/src/BizHawk.Bizware.Input/KeyInput/X11KeyInput.cs +++ b/src/BizHawk.Bizware.Input/KeyInput/X11KeyInput.cs @@ -31,7 +31,10 @@ public X11KeyInput() if (Display == IntPtr.Zero) { - throw new("Could not open XDisplay"); + // There doesn't seem to be a convention for what exception type to throw in these situations. Can't use NRE. Well... +// _ = Unsafe.AsRef()!; // hmm + // InvalidOperationException doesn't match. Exception it is. --yoshi + throw new Exception("Could not open XDisplay"); } using (new XLock(Display)) diff --git a/src/BizHawk.Bizware.Input/SDL2/SDL2InputAdapter.cs b/src/BizHawk.Bizware.Input/SDL2/SDL2InputAdapter.cs index 297a735bc0..d9a3d1415c 100644 --- a/src/BizHawk.Bizware.Input/SDL2/SDL2InputAdapter.cs +++ b/src/BizHawk.Bizware.Input/SDL2/SDL2InputAdapter.cs @@ -9,6 +9,8 @@ using static SDL2.SDL; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Bizware.Input { public sealed class SDL2InputAdapter : OSTailoredKeyInputAdapter diff --git a/src/BizHawk.Client.Common/XmlGame.cs b/src/BizHawk.Client.Common/XmlGame.cs index 19ffd70af3..2d12f9d89c 100644 --- a/src/BizHawk.Client.Common/XmlGame.cs +++ b/src/BizHawk.Client.Common/XmlGame.cs @@ -66,7 +66,7 @@ public static XmlGame Create(HawkFile f) } else { - throw new($"Couldn't load XMLGame Asset \"{filename}\""); + throw new Exception($"Couldn't load XMLGame Asset \"{filename}\""); } } else @@ -96,7 +96,7 @@ public static XmlGame Create(HawkFile f) } catch (Exception e) { - throw new($"Couldn't load XMLGame LoadAsset \"{filename}\"", e); + throw new Exception($"Couldn't load XMLGame LoadAsset \"{filename}\"", e); } } diff --git a/src/BizHawk.Client.EmuHawk/AVOut/AviWriter.cs b/src/BizHawk.Client.EmuHawk/AVOut/AviWriter.cs index 24a84bf05f..57605734ca 100644 --- a/src/BizHawk.Client.EmuHawk/AVOut/AviWriter.cs +++ b/src/BizHawk.Client.EmuHawk/AVOut/AviWriter.cs @@ -168,7 +168,7 @@ public void AddFrame(IVideoProvider source) { if (!_workerT.IsAlive) { - throw new("AVI Worker thread died!"); + throw new Exception("AVI Worker thread died!"); } } } @@ -194,7 +194,7 @@ public void AddSamples(short[] samples) { if (!_workerT.IsAlive) { - throw new("AVI Worker thread died!"); + throw new Exception("AVI Worker thread died!"); } } } @@ -977,7 +977,7 @@ public unsafe void AddFrame(IVideoProvider source) public void SetDefaultVideoCodecToken(Config config) { var ct = CodecToken.DeSerialize(config.AviCodecToken); - _currVideoCodecToken = ct ?? throw new($"No default {nameof(config.AviCodecToken)} in config!"); + _currVideoCodecToken = ct ?? throw new Exception($"No default {nameof(config.AviCodecToken)} in config!"); } public string DesiredExtension() diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Debug.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Debug.cs index 72d03b4a90..d1015530dd 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Debug.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.Debug.cs @@ -11,6 +11,8 @@ using BizHawk.Emulation.Cores.Arcades.MAME; using BizHawk.Emulation.DiscSystem; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Client.EmuHawk { public partial class RCheevos diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs index f2801c30d8..cf9c59ee6d 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevos.cs @@ -10,6 +10,8 @@ using BizHawk.Client.Common; using BizHawk.Emulation.Common; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Client.EmuHawk { public partial class RCheevos : RetroAchievements diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.GameVerification.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.GameVerification.cs index 7155fb10b4..b79dfc2fe3 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.GameVerification.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.GameVerification.cs @@ -13,6 +13,8 @@ using BizHawk.Emulation.Common; using BizHawk.Emulation.DiscSystem; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Client.EmuHawk { public abstract partial class RetroAchievements diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs index 0490799b8f..8e0cb22239 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs @@ -191,7 +191,7 @@ public void Restart( lib.APIs = _apiContainer; if (!ServiceInjector.UpdateServices(newServiceProvider, lib, mayCache: true)) { - throw new("Lua lib has required service(s) that can't be fulfilled"); + throw new Exception("Lua lib has required service(s) that can't be fulfilled"); } lib.Restarted(); diff --git a/src/BizHawk.Common/Extensions/PathExtensions.cs b/src/BizHawk.Common/Extensions/PathExtensions.cs index c013fdc592..0a34231f68 100644 --- a/src/BizHawk.Common/Extensions/PathExtensions.cs +++ b/src/BizHawk.Common/Extensions/PathExtensions.cs @@ -219,7 +219,9 @@ static PathUtils() else { var dirPath = AppContext.BaseDirectory; - DataDirectoryPath = ExeDirectoryPath = string.IsNullOrEmpty(dirPath) ? throw new("failed to get location of executable, very bad things must have happened") : dirPath.RemoveSuffix('\\'); + DataDirectoryPath = ExeDirectoryPath = string.IsNullOrEmpty(dirPath) + ? throw new Exception("failed to get location of executable, very bad things must have happened") + : dirPath.RemoveSuffix('\\'); DllDirectoryPath = Path.Combine(ExeDirectoryPath, "dll"); } } diff --git a/src/BizHawk.Common/OSTailoredCode.cs b/src/BizHawk.Common/OSTailoredCode.cs index ebb6c7cd23..dc855a7f59 100644 --- a/src/BizHawk.Common/OSTailoredCode.cs +++ b/src/BizHawk.Common/OSTailoredCode.cs @@ -279,7 +279,7 @@ public static string SimpleSubshell(string cmd, string args, string noOutputMsg) using var proc = ConstructSubshell(cmd, args); proc.Start(); var stdout = proc.StandardOutput; - if (stdout.EndOfStream) throw new($"{noOutputMsg} ({cmd} wrote nothing to stdout)"); + if (stdout.EndOfStream) throw new Exception($"{noOutputMsg} ({cmd} wrote nothing to stdout)"); return stdout.ReadLine()!; } } diff --git a/src/BizHawk.Emulation.Common/Sound/SDLResampler.cs b/src/BizHawk.Emulation.Common/Sound/SDLResampler.cs index b4fa081419..8f2945a3b1 100644 --- a/src/BizHawk.Emulation.Common/Sound/SDLResampler.cs +++ b/src/BizHawk.Emulation.Common/Sound/SDLResampler.cs @@ -4,6 +4,8 @@ using static SDL2.SDL; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Emulation.Common { /// diff --git a/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs b/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs index 6c0f486bce..c5fba2de3d 100644 --- a/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs +++ b/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs @@ -88,7 +88,7 @@ public MAME(CoreLoadParameters lp) if (_loadFailure != string.Empty) { Dispose(); - throw new("\n\n" + _loadFailure); + throw new Exception("\n\n" + _loadFailure); } // concat all SHA1 hashes together (unprefixed), then hash that diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs index 9d1d318f8c..6ba13534b7 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs @@ -8,6 +8,8 @@ using BizHawk.Common; using BizHawk.Emulation.Common; +#pragma warning disable BHI1007 // target-typed Exception TODO don't + namespace BizHawk.Emulation.Cores.Consoles.Nintendo.N3DS { [PortedCore(CoreNames.Encore, "", "nightly-2104", "https://github.com/CasualPokePlayer/encore", singleInstance: true)] diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs index 414d896fdd..8dcc3d0b31 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs @@ -385,7 +385,7 @@ private static byte[] DecideNAND(ICoreFileProvider cfp, bool isDSiEnhanced, byte private static byte[] GetTMDData(ulong titleId) { using var zip = new ZipArchive(Zstd.DecompressZstdStream(new MemoryStream(Resources.TMDS.Value)), ZipArchiveMode.Read, false); - using var tmd = zip.GetEntry($"{titleId:x16}.tmd")?.Open() ?? throw new($"Cannot find TMD for title ID {titleId:x16}, please report"); + using var tmd = zip.GetEntry($"{titleId:x16}.tmd")?.Open() ?? throw new Exception($"Cannot find TMD for title ID {titleId:x16}, please report"); return tmd.ReadAllBytes(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.cs b/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.cs index 40f6390b11..96ee138756 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGX.cs @@ -91,7 +91,7 @@ public GPGX(CoreLoadParameters lp) { if (lp.Discs.Count > 128) { - throw new("Too many discs loaded at once!"); + throw new ArgumentException(paramName: nameof(lp), message: "Too many discs loaded at once!"); } _cds = lp.Discs.Select(d => d.DiscData).ToArray();