From a2fef59fe188df3f2f3250c13d6e6a49cf1c463c Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 19 Jul 2022 07:15:53 +1000 Subject: [PATCH] Add Analyzer rule to prohibit `typeof(T).ToString()` --- Common.ruleset | 6 ++ .../UseNameofOperatorAnalyzer.cs | 69 ++++++++++++++++++ References/BizHawk.Analyzer.dll | Bin 20992 -> 23040 bytes .../tools/ToolManager.cs | 2 +- .../Hardware/Datacorder/DatacorderDevice.cs | 4 +- .../AmstradCPC/Hardware/Disk/NECUPD765.FDD.cs | 3 +- .../AmstradCPC/Media/MediaConverter.cs | 17 +---- .../AmstradCPC/Media/Tape/CDT/CdtConverter.cs | 7 +- .../Hardware/Datacorder/DatacorderDevice.cs | 20 ++--- .../Hardware/Disk/NECUPD765.FDD.cs | 3 +- .../SinclairSpectrum/Media/MediaConverter.cs | 17 +---- .../Media/Tape/CSW/CswConverter.cs | 16 ++-- .../Media/Tape/PZX/PzxConverter.cs | 7 +- .../Media/Tape/TAP/TapConverter.cs | 4 +- .../Media/Tape/TZX/TzxConverter.cs | 7 +- .../Media/Tape/WAV/WavConverter.cs | 7 +- .../Consoles/Nintendo/QuickNES/QuickNES.cs | 2 +- .../Consoles/Sega/gpgx64/GenDbgHlp.cs | 6 +- 18 files changed, 116 insertions(+), 81 deletions(-) create mode 100644 ExternalProjects/BizHawk.Analyzer/UseNameofOperatorAnalyzer.cs diff --git a/Common.ruleset b/Common.ruleset index 640e31b866..147a92f576 100644 --- a/Common.ruleset +++ b/Common.ruleset @@ -22,6 +22,12 @@ + + + + + + diff --git a/ExternalProjects/BizHawk.Analyzer/UseNameofOperatorAnalyzer.cs b/ExternalProjects/BizHawk.Analyzer/UseNameofOperatorAnalyzer.cs new file mode 100644 index 0000000000..31140b2d67 --- /dev/null +++ b/ExternalProjects/BizHawk.Analyzer/UseNameofOperatorAnalyzer.cs @@ -0,0 +1,69 @@ +namespace BizHawk.Analyzers; + +using System.Collections.Immutable; + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; + +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class UseNameofOperatorAnalyzer : DiagnosticAnalyzer +{ + private static readonly DiagnosticDescriptor DiagNoToStringOnType = new( + id: "BHI1103", + title: "Don't call typeof(T).ToString(), use nameof operator or typeof(T).FullName", + messageFormat: "Replace typeof({0}){1} with either nameof({0}) or typeof({0}).FullName", + category: "Usage", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true); + + private static readonly DiagnosticDescriptor DiagUseNameof = new( + id: "BHI1102", + title: "Don't call typeof(T).Name, use nameof operator", + messageFormat: "Replace typeof({0}).Name with nameof({0})", + category: "Usage", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true); + + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(DiagNoToStringOnType, DiagUseNameof); + + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); + context.EnableConcurrentExecution(); + ISymbol? memberInfoDotNameSym = null; + ISymbol? typeDotToStringSym = null; + context.RegisterSyntaxNodeAction( + snac => + { + memberInfoDotNameSym ??= snac.Compilation.GetTypeByMetadataName("System.Reflection.MemberInfo")!.GetMembers("Name")[0]; + typeDotToStringSym ??= snac.Compilation.GetTypeByMetadataName("System.Type")!.GetMembers("ToString")[0]; + var toes = (TypeOfExpressionSyntax) snac.Node; + switch (toes.Parent) + { + case BinaryExpressionSyntax bes: + if ((ReferenceEquals(toes, bes.Left) ? bes.Right : bes.Left) is LiteralExpressionSyntax { Token.RawKind: (int) SyntaxKind.StringLiteralToken }) + { + snac.ReportDiagnostic(Diagnostic.Create(DiagNoToStringOnType, toes.GetLocation(), toes.Type.GetText(), " in string concatenation")); + } + break; + case InterpolationSyntax: + snac.ReportDiagnostic(Diagnostic.Create(DiagNoToStringOnType, toes.GetLocation(), toes.Type.GetText(), " in string interpolation")); + break; + case MemberAccessExpressionSyntax maes1: + var accessed = snac.SemanticModel.GetSymbolInfo(maes1.Name).Symbol; + if (memberInfoDotNameSym.Matches(accessed)) + { + snac.ReportDiagnostic(Diagnostic.Create(DiagUseNameof, maes1.GetLocation(), toes.Type.GetText())); + } + else if (typeDotToStringSym.Matches(accessed)) + { + snac.ReportDiagnostic(Diagnostic.Create(DiagNoToStringOnType, maes1.GetLocation(), toes.Type.GetText(), ".ToString()")); + } + break; + } + }, + SyntaxKind.TypeOfExpression); + } +} diff --git a/References/BizHawk.Analyzer.dll b/References/BizHawk.Analyzer.dll index cfa7f15ff033e5a547ba1c44c0e604b2f06e4650..54b9ce45fa1cda9d5998164a7fb16f80499853fa 100644 GIT binary patch delta 8674 zcmb7Jd3aRixqrWNX3m^*CbMUSKqd*0AsbsnLAD4kksSgOz$JtLVVf|SR4|5_5bG6% zN<3FuuN6_e3SOT_F+AG(sDNnIDiwMaHGvR1$WJkwo*r&}`<34LuJo^lsTeG+Q!~L~GTa;!xIZ zqAbuGTZ!teVx`h*5D{Z8IAMI}SvQLcQExpa%ETh;ZBbJ27#zsYW%w%Oq(4;?ZL1{` zmY9CNHAe}GTB}K!IB_%45pr0Q5Eo0TCxSok-WAMDmeS zIbs`jEfFtv)IqJWKS&U9>zB^LELB50Bw>Sgn68tm4S`TT33CJT?PT%7{$hvC?GwAOe{3e< zdr*Hilhth_P|RdlimcOGU;&m!pl!6QzNSVoBh1)&2DL^}db4u~w6qS1RiZpC{?CjU zla7aHj2s?0M+c?#~SUb6Hk(lIMT5Xpuf9VHdUrL>ti&kE?-BhLq`@*2&EA_3E|)z|}Awo|F#0hKaZ zpbr-pS_wTrN9M5^2|Cog?U*r`QnAy9^^jhi$VeO+$#o{R-Wx#zW(4lda-~D|rh~g5 z2_Qz*5as%F*e!7PxBEifhL8g{7sI;GzJTPkHWUN%8J5*Q5yeh7f`xCoB7Veoafu>h(X_V|7w?;f zHP_5u^e=kUB0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=NRXN-Ftz5!Q9u&XSUxUw!Sr};fWS|onE^^<5MiqKSHd{k-w|GXoPwWOc!xcO zP#qIO6{HE3O%rYc=`^9zMc7awZluw6h3JM;h3HeYv!;PH4Xl|VRKu;%Uj|g3t*q~Z zrpfMOO&@C>5NiDPLch}9%lf^n-^Uq_z?ukJgB&)<=0P@})57T%QFj$Cp9kP@MyeQ67z>b@V=s72RTs_z!Vdd?Y5zm*vS-_??PA0G5!R zNUG1v`PV6Yj^ufYPvztMan;br;p;1z`b!BPv&^MyUw(_<9sC*2L-F~z3vd@=bc&Fx z7#q-diq63%vY0NTZ8VkUP!sMJ$Xzrfdhi2DJ*^!$Rerzj-8uHuKyR!~3x99T@|Co( ztE02+rrPFJ?QP3Fr}_r3yLv@-{DvR4wRX;kcg}3EXcVyyJuXs|vH*-?{z`99u#K2RNU)KkI zGIgiv<;<+XFB^aSmMPG4Gz6)D~1tN5oPSNPc}dEAMl{gcN_ z!@bc^wvc(@CQ*hLT}H}tu`fby?>JvXxa%D&LV4ld8sVG+UHF3MY?h5pwhh{>RZbLWj z$#Z*`t1gz5Ab0Xls906RwQqKlZz5iXlfU4cZP-TBMh6wK7aMNE>_rZTG&ec0b#}hb zw@NeIX78c-QL)&2asDgf_`oj~oDle}kc=lDyNATw1!@*<#r?`k+>M!u#`84?R2@8* zVHz)+?fv?y^6cs-SKj+t&7rw>yiznbI5h6Cck-&+YSYS_W~Y_No%i?99fy{_9ox}b z{G+qKM*Q&g{3>tT!eV)NmG{WP3Es(twR public virtual bool IsWriter => false; - protected abstract Type SelfType { get; } + protected abstract string SelfTypeName { get; } /// /// Serialization method /// public virtual void Read(byte[] data) - { - throw new NotImplementedException(SelfType + - "Read operation is not implemented for this converter"); - } + => throw new NotImplementedException($"Read operation is not implemented for {SelfTypeName}"); /// /// DeSerialization method /// public virtual void Write(byte[] data) - { - throw new NotImplementedException(SelfType + - "Write operation is not implemented for this converter"); - } + => throw new NotImplementedException($"Write operation is not implemented for {SelfTypeName}"); /// /// Serializer does a quick check, returns TRUE if file is detected as this type /// public virtual bool CheckType(byte[] data) - { - throw new NotImplementedException(SelfType.ToString() + - "Check type operation is not implemented for this converter"); - } + => throw new NotImplementedException($"Check type operation is not implemented for {SelfTypeName}"); /// /// Converts an int32 value into a byte array diff --git a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs index 2e803041b1..dd03153e30 100644 --- a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Media/Tape/CDT/CdtConverter.cs @@ -26,8 +26,8 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC /// public override bool IsWriter => false; - protected override Type SelfType - => typeof(CdtConverter); + protected override string SelfTypeName + => nameof(CdtConverter); /// /// Working list of generated tape data blocks @@ -167,8 +167,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC if (ident != "ZXTape!" || eotm != 0x1A) { // this is not a valid TZX format file - throw new Exception(typeof(CdtConverter) + - "This is not a valid TZX format file"); + throw new Exception($"{nameof(CdtConverter)}: This is not a valid TZX format file"); } // iterate through each block diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Datacorder/DatacorderDevice.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Datacorder/DatacorderDevice.cs index 37e225e404..e50989e972 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Datacorder/DatacorderDevice.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Datacorder/DatacorderDevice.cs @@ -311,9 +311,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum catch (Exception ex) { // exception during operation - var e = ex; - throw new Exception(typeof(DatacorderDevice).ToString() + - "\n\nTape image file has a valid TZX header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + throw new Exception($"{nameof(DatacorderDevice)}\n\nTape image file has a valid TZX header, but threw an exception whilst data was being parsed.\n\n{ex}", ex); } } @@ -331,9 +329,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum catch (Exception ex) { // exception during operation - var e = ex; - throw new Exception(typeof(DatacorderDevice).ToString() + - "\n\nTape image file has a valid PZX header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + throw new Exception($"{nameof(DatacorderDevice)}\n\nTape image file has a valid PZX header, but threw an exception whilst data was being parsed.\n\n{ex}", ex); } } @@ -351,9 +347,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum catch (Exception ex) { // exception during operation - var e = ex; - throw new Exception(typeof(DatacorderDevice).ToString() + - "\n\nTape image file has a valid CSW header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + throw new Exception($"{nameof(DatacorderDevice)}\n\nTape image file has a valid CSW header, but threw an exception whilst data was being parsed.\n\n{ex}", ex); } } @@ -371,9 +365,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum catch (Exception ex) { // exception during operation - var e = ex; - throw new Exception(typeof(DatacorderDevice).ToString() + - "\n\nTape image file has a valid WAV header, but threw an exception whilst data was being parsed.\n\n" + e.ToString()); + throw new Exception($"{nameof(DatacorderDevice)}\n\nTape image file has a valid WAV header, but threw an exception whilst data was being parsed.\n\n{ex}", ex); } } @@ -390,9 +382,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum catch (Exception ex) { // exception during operation - var e = ex; - throw new Exception(typeof(DatacorderDevice).ToString() + - "\n\nAn exception was thrown whilst data from this tape image was being parsed as TAP.\n\n" + e.ToString()); + throw new Exception($"{nameof(DatacorderDevice)}\n\nAn exception was thrown whilst data from this tape image was being parsed as TAP.\n\n{ex}", ex); } } } diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDD.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDD.cs index c4aee7d40a..8911b943d0 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDD.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDD.cs @@ -776,8 +776,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum if (!found) { - throw new Exception(typeof(DriveState).ToString() + - "\n\nDisk image file could not be parsed. Potentially an unknown format."); + throw new Exception($"{nameof(DriveState)}\n\nDisk image file could not be parsed. Potentially an unknown format."); } } diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverter.cs index 5425fd46f1..1606160428 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/MediaConverter.cs @@ -26,34 +26,25 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// public virtual bool IsWriter => false; - protected abstract Type SelfType { get; } + protected abstract string SelfTypeName { get; } /// /// Serialization method /// public virtual void Read(byte[] data) - { - throw new NotImplementedException(SelfType.ToString() + - "Read operation is not implemented for this converter"); - } + => throw new NotImplementedException($"Read operation is not implemented for {SelfTypeName}"); /// /// DeSerialization method /// public virtual void Write(byte[] data) - { - throw new NotImplementedException(SelfType.ToString() + - "Write operation is not implemented for this converter"); - } + => throw new NotImplementedException($"Write operation is not implemented for {SelfTypeName}"); /// /// Serializer does a quick check, returns TRUE if file is detected as this type /// public virtual bool CheckType(byte[] data) - { - throw new NotImplementedException(SelfType.ToString() + - "Check type operation is not implemented for this converter"); - } + => throw new NotImplementedException($"Check type operation is not implemented for {SelfTypeName}"); /// /// Converts an int32 value into a byte array diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs index 97429ef4e1..a9dff3ce7e 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs @@ -32,8 +32,8 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// public override bool IsWriter => false; - protected override Type SelfType - => typeof(CswConverter); + protected override string SelfTypeName + => nameof(CswConverter); private readonly DatacorderDevice _datacorder; @@ -83,15 +83,13 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum if (ident.ToUpper() != "COMPRESSED SQUARE WAVE") { // this is not a valid CSW format file - throw new Exception(typeof(CswConverter).ToString() + - "This is not a valid CSW format file"); + throw new Exception($"{nameof(CswConverter)}: This is not a valid CSW format file"); } if (data[0x16] != 0x1a) { // invalid terminator code - throw new Exception(typeof(CswConverter).ToString() + - "This image reports as a CSW but has an invalid terminator code"); + throw new Exception($"{nameof(CswConverter)}: This image reports as a CSW but has an invalid terminator code"); } _position = 0; @@ -185,13 +183,11 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum if (compressionType == 1) Array.Copy(data, _position, cswDataUncompressed, 0, cswDataUncompressed.Length); else - throw new Exception(typeof(CswConverter).ToString() + - "CSW Format unknown compression type"); + throw new Exception($"{nameof(CswConverter)}: CSW Format unknown compression type"); } else { - throw new Exception(typeof(CswConverter).ToString() + - "CSW Format Version " + majorVer + "." + minorVer + " is not currently supported"); + throw new Exception($"{nameof(CswConverter)}: CSW Format Version {majorVer}.{minorVer} is not currently supported"); } // create the single tape block diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs index 4d92fd8e50..37c3333207 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs @@ -27,8 +27,8 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// public override bool IsWriter => false; - protected override Type SelfType - => typeof(PzxConverter); + protected override string SelfTypeName + => nameof(PzxConverter); /// /// Working list of generated tape data blocks @@ -99,8 +99,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum if (ident.ToUpper() != "PZXT") { // this is not a valid TZX format file - throw new Exception(typeof(PzxConverter).ToString() + - "This is not a valid PZX format file"); + throw new Exception($"{nameof(PzxConverter)}: This is not a valid PZX format file"); } _position = 0; diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TAP/TapConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TAP/TapConverter.cs index 7e3906788f..dca42b861a 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TAP/TapConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TAP/TapConverter.cs @@ -27,8 +27,8 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// public override bool IsWriter => false; - protected override Type SelfType - => typeof(TapConverter); + protected override string SelfTypeName + => nameof(TapConverter); private readonly DatacorderDevice _datacorder; diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TZX/TzxConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TZX/TzxConverter.cs index b3373509fb..9da1fd9a24 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TZX/TzxConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/TZX/TzxConverter.cs @@ -26,8 +26,8 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// public override bool IsWriter => false; - protected override Type SelfType - => typeof(TzxConverter); + protected override string SelfTypeName + => nameof(TzxConverter); /// /// Working list of generated tape data blocks @@ -205,8 +205,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum if (ident != "ZXTape!" || eotm != 0x1A) { // this is not a valid TZX format file - throw new Exception(typeof(TzxConverter) + - "This is not a valid TZX format file"); + throw new Exception($"{nameof(TzxConverter)}: This is not a valid TZX format file"); } // iterate through each block diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs index aa6e13c2d2..9b9f8365fb 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs @@ -28,8 +28,8 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum /// public override bool IsWriter => false; - protected override Type SelfType - => typeof(WavConverter); + protected override string SelfTypeName + => nameof(WavConverter); /// /// Position counter @@ -77,8 +77,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum if (ident.ToUpper() != "WAVE") { // this is not a valid TZX format file - throw new Exception(typeof(WavConverter).ToString() + - "This is not a valid WAV format file"); + throw new Exception($"{nameof(WavConverter)}: This is not a valid WAV format file"); } //_position = 0; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/QuickNES/QuickNES.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/QuickNES/QuickNES.cs index bb8c880487..651f999c11 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/QuickNES/QuickNES.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/QuickNES/QuickNES.cs @@ -293,7 +293,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES private void CheckDisposed() { if (Context == IntPtr.Zero) - throw new ObjectDisposedException(typeof(QuickNES).Name); + throw new ObjectDisposedException(nameof(QuickNES)); } // Fix some incorrect ines header entries that QuickNES uses to load games. diff --git a/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GenDbgHlp.cs b/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GenDbgHlp.cs index 6a326a70a0..0fc56a0c36 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GenDbgHlp.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GenDbgHlp.cs @@ -49,7 +49,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx public void SaveState(int statenum) { - if (disposed) throw new ObjectDisposedException(typeof(GenDbgHlp).ToString()); + if (disposed) throw new ObjectDisposedException(nameof(GenDbgHlp)); data[statenum] ??= new byte[length]; @@ -60,7 +60,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx public unsafe void Cmp(int statex, int statey) { - if (disposed) throw new ObjectDisposedException(typeof(GenDbgHlp).ToString()); + if (disposed) throw new ObjectDisposedException(nameof(GenDbgHlp)); List> bads = new List>(); byte[] x = data[statex]; @@ -134,7 +134,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx public List Find(IntPtr addr, int length) { - if (disposed) throw new ObjectDisposedException(typeof(GenDbgHlp).ToString()); + if (disposed) throw new ObjectDisposedException(nameof(GenDbgHlp)); Symbol min = new Symbol { addr = addr }; Symbol max = new Symbol { addr = addr + length };