Enable CA1862 and fix noncompliance

"Use the 'StringComparison' method overloads to perform case-insensitive
string comparisons"
This commit is contained in:
YoshiRulz 2025-01-29 19:30:04 +10:00
parent 96207e80ea
commit 12cd7885ec
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
18 changed files with 40 additions and 23 deletions

View File

@ -73,6 +73,8 @@ dotnet_diagnostic.CA1822.severity = silent
dotnet_code_quality.CA1826.exclude_ordefault_methods = true dotnet_code_quality.CA1826.exclude_ordefault_methods = true
# Avoid StringBuilder parameters for P/Invokes # Avoid StringBuilder parameters for P/Invokes
dotnet_diagnostic.CA1838.severity = suggestion dotnet_diagnostic.CA1838.severity = suggestion
# Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
dotnet_diagnostic.CA1862.severity = error
## Usage rules ## Usage rules

View File

@ -63,7 +63,9 @@ namespace BizHawk.Client.Common
NotInDatabase = true NotInDatabase = true
}; };
#pragma warning disable CA1862 // testing whether it's all-caps
if (!string.IsNullOrWhiteSpace(GameInfo.Name) && GameInfo.Name == GameInfo.Name.ToUpperInvariant()) if (!string.IsNullOrWhiteSpace(GameInfo.Name) && GameInfo.Name == GameInfo.Name.ToUpperInvariant())
#pragma warning restore CA1862
{ {
GameInfo.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(GameInfo.Name.ToLowerInvariant()); GameInfo.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(GameInfo.Name.ToLowerInvariant());
} }

View File

@ -655,7 +655,7 @@ namespace BizHawk.Client.Common
private static bool IsDiscForXML(string system, string path) private static bool IsDiscForXML(string system, string path)
{ {
var ext = Path.GetExtension(path); var ext = Path.GetExtension(path);
if (system == VSystemID.Raw.Arcade && ext.ToLowerInvariant() == ".chd") if (system is VSystemID.Raw.Arcade && ".chd".EqualsIgnoreCase(ext))
{ {
return false; return false;
} }

View File

@ -4,6 +4,7 @@ using System.Drawing.Imaging;
using BizHawk.Bizware.Graphics; using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Common.PathExtensions; using BizHawk.Common.PathExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
@ -49,11 +50,11 @@ namespace BizHawk.Client.EmuHawk
var name = Path.Combine(dir!, $"{fileNoExt}_{_frame}{ext}"); var name = Path.Combine(dir!, $"{fileNoExt}_{_frame}{ext}");
BitmapBuffer bb = new BitmapBuffer(source.BufferWidth, source.BufferHeight, source.GetVideoBuffer()); BitmapBuffer bb = new BitmapBuffer(source.BufferWidth, source.BufferHeight, source.GetVideoBuffer());
using var bmp = bb.ToSysdrawingBitmap(); using var bmp = bb.ToSysdrawingBitmap();
if (ext.ToUpperInvariant() == ".PNG") if (".PNG".EqualsIgnoreCase(ext))
{ {
bmp.Save(name, ImageFormat.Png); bmp.Save(name, ImageFormat.Png);
} }
else if (ext.ToUpperInvariant() == ".JPG") else if (".JPG".EqualsIgnoreCase(ext))
{ {
bmp.Save(name, ImageFormat.Jpeg); bmp.Save(name, ImageFormat.Jpeg);
} }

View File

@ -6,7 +6,7 @@ using System.Text;
using System.Threading; using System.Threading;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
@ -600,7 +600,7 @@ namespace BizHawk.Client.EmuHawk
public void OpenFile(string baseName) public void OpenFile(string baseName)
{ {
string ext = Path.GetExtension(baseName); string ext = Path.GetExtension(baseName);
if (ext == null || ext.ToLowerInvariant() != ".jmd") if (ext?.EqualsIgnoreCase(".jmd") is true or null)
{ {
baseName += ".jmd"; baseName += ".jmd";
} }

View File

@ -5,6 +5,7 @@ using System.Text.RegularExpressions;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
@ -33,9 +34,7 @@ namespace BizHawk.Client.EmuHawk
lvi.SubItems.Add(new ListViewItem.ListViewSubItem()); lvi.SubItems.Add(new ListViewItem.ListViewSubItem());
lvi.Text = item.Name; lvi.Text = item.Name;
long size = item.Size; long size = item.Size;
var extension = Path.GetExtension(item.Name); if (size % 1024 is 16 && Path.GetExtension(item.Name)?.EqualsIgnoreCase(".NES") is true) size -= 16;
if (extension != null && size % 1024 == 16 && extension.ToUpperInvariant() == ".NES")
size -= 16;
lvi.SubItems[1].Text = Util.FormatFileSize(size); lvi.SubItems[1].Text = Util.FormatFileSize(size);
_archiveItems.Add(lvi); _archiveItems.Add(lvi);
} }

View File

@ -1388,7 +1388,7 @@ namespace BizHawk.Client.EmuHawk
using (var bb = Config.ScreenshotCaptureOsd ? CaptureOSD() : MakeScreenshotImage()) using (var bb = Config.ScreenshotCaptureOsd ? CaptureOSD() : MakeScreenshotImage())
{ {
using var img = bb.ToSysdrawingBitmap(); using var img = bb.ToSysdrawingBitmap();
if (Path.GetExtension(path).ToUpperInvariant() == ".JPG") if (".JPG".EqualsIgnoreCase(Path.GetExtension(path)))
{ {
img.Save(fi.FullName, ImageFormat.Jpeg); img.Save(fi.FullName, ImageFormat.Jpeg);
} }
@ -3724,7 +3724,8 @@ namespace BizHawk.Client.EmuHawk
InputManager.SyncControls(Emulator, MovieSession, Config); InputManager.SyncControls(Emulator, MovieSession, Config);
_multiDiskMode = false; _multiDiskMode = false;
if (oaOpenrom != null && Path.GetExtension(oaOpenrom.Path.Replace("|", "")).ToLowerInvariant() == ".xml" && Emulator is not LibsnesCore) if (oaOpenrom is not null && ".xml".EqualsIgnoreCase(Path.GetExtension(oaOpenrom.Path.Replace("|", "")))
&& Emulator is not LibsnesCore)
{ {
// this is a multi-disk bundler file // this is a multi-disk bundler file
// determine the xml assets and create RomStatusDetails for all of them // determine the xml assets and create RomStatusDetails for all of them

View File

@ -487,7 +487,9 @@ namespace BizHawk.Emulation.Common
game.Name = Path.GetFileNameWithoutExtension(fileName)?.Replace('_', ' '); game.Name = Path.GetFileNameWithoutExtension(fileName)?.Replace('_', ' ');
// If filename is all-caps, then attempt to proper-case the title. // If filename is all-caps, then attempt to proper-case the title.
#pragma warning disable CA1862 // testing whether it's all-caps
if (!string.IsNullOrWhiteSpace(game.Name) && game.Name == game.Name.ToUpperInvariant()) if (!string.IsNullOrWhiteSpace(game.Name) && game.Name == game.Name.ToUpperInvariant())
#pragma warning restore CA1862
{ {
game.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(game.Name.ToLowerInvariant()); game.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(game.Name.ToLowerInvariant());
} }

View File

@ -251,7 +251,7 @@ namespace BizHawk.Emulation.Cores.Arcades.MAME
foreach (var rom in roms) foreach (var rom in roms)
{ {
// only close non-chd files // only close non-chd files
if (rom.Extension.ToLowerInvariant() != ".chd") if (!".chd".EqualsIgnoreCase(rom.Extension))
{ {
_exe.RemoveReadonlyFile(MakeFileName(rom)); _exe.RemoveReadonlyFile(MakeFileName(rom));
} }

View File

@ -3,6 +3,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
{ {
/// <summary> /// <summary>
@ -57,7 +59,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
int majorVer = data[8]; int majorVer = data[8];
int minorVer = data[9]; int minorVer = data[9];
if (ident.ToUpperInvariant() != "COMPRESSED SQUARE WAVE") if (!"COMPRESSED SQUARE WAVE".EqualsIgnoreCase(ident))
{ {
// this is not a valid CSW format file // this is not a valid CSW format file
return false; return false;
@ -80,7 +82,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// (first 22 bytes of the file) // (first 22 bytes of the file)
string ident = Encoding.ASCII.GetString(data, 0, 22); string ident = Encoding.ASCII.GetString(data, 0, 22);
if (ident.ToUpperInvariant() != "COMPRESSED SQUARE WAVE") if (!"COMPRESSED SQUARE WAVE".EqualsIgnoreCase(ident))
{ {
// this is not a valid CSW format file // this is not a valid CSW format file
throw new Exception($"{nameof(CswConverter)}: This is not a valid CSW format file"); throw new Exception($"{nameof(CswConverter)}: This is not a valid CSW format file");

View File

@ -3,6 +3,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
{ {
/// <summary> /// <summary>
@ -67,7 +69,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
int majorVer = data[8]; int majorVer = data[8];
int minorVer = data[9]; int minorVer = data[9];
if (ident.ToUpperInvariant() != "PZXT") if (!"PZXT".EqualsIgnoreCase(ident))
{ {
// this is not a valid PZX format file // this is not a valid PZX format file
return false; return false;
@ -96,7 +98,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// check whether this is a valid pzx format file by looking at the identifier in the header block // check whether this is a valid pzx format file by looking at the identifier in the header block
string ident = Encoding.ASCII.GetString(data, 0, 4); string ident = Encoding.ASCII.GetString(data, 0, 4);
if (ident.ToUpperInvariant() != "PZXT") if (!"PZXT".EqualsIgnoreCase(ident))
{ {
// this is not a valid TZX format file // this is not a valid TZX format file
throw new Exception($"{nameof(PzxConverter)}: This is not a valid PZX format file"); throw new Exception($"{nameof(PzxConverter)}: This is not a valid PZX format file");

View File

@ -2,6 +2,8 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
{ {
/// <summary> /// <summary>
@ -50,7 +52,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// check whether this is a valid wav format file by looking at the identifier in the header // check whether this is a valid wav format file by looking at the identifier in the header
string ident = Encoding.ASCII.GetString(data, 8, 4); string ident = Encoding.ASCII.GetString(data, 8, 4);
if (ident.ToUpperInvariant() != "WAVE") if (!"WAVE".EqualsIgnoreCase(ident))
{ {
// this is not a valid WAV format file // this is not a valid WAV format file
return false; return false;
@ -72,7 +74,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// check whether this is a valid pzx format file by looking at the identifier in the header block // check whether this is a valid pzx format file by looking at the identifier in the header block
string ident = Encoding.ASCII.GetString(data, 8, 4); string ident = Encoding.ASCII.GetString(data, 8, 4);
if (ident.ToUpperInvariant() != "WAVE") if (!"WAVE".EqualsIgnoreCase(ident))
{ {
// this is not a valid TZX format file // this is not a valid TZX format file
throw new Exception($"{nameof(WavConverter)}: This is not a valid WAV format file"); throw new Exception($"{nameof(WavConverter)}: This is not a valid WAV format file");

View File

@ -6,6 +6,7 @@ using System.Text;
using BizHawk.BizInvoke; using BizHawk.BizInvoke;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
#pragma warning disable BHI1007 // target-typed Exception TODO don't #pragma warning disable BHI1007 // target-typed Exception TODO don't
@ -122,7 +123,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.N3DS
_serviceProvider.Register<IVideoProvider>(_encoreVideoProvider); _serviceProvider.Register<IVideoProvider>(_encoreVideoProvider);
var romPath = lp.Roms[0].RomPath; var romPath = lp.Roms[0].RomPath;
if (lp.Roms[0].Extension.ToLowerInvariant() == ".cia") if (".cia".EqualsIgnoreCase(lp.Roms[0].Extension))
{ {
var message = new byte[1024]; var message = new byte[1024];
var res = _core.Encore_InstallCIA(_context, romPath, message, message.Length); var res = _core.Encore_InstallCIA(_context, romPath, message, message.Length);
@ -145,7 +146,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.N3DS
for (var i = 1; i < lp.Roms.Count; i++) for (var i = 1; i < lp.Roms.Count; i++)
{ {
// doesn't make sense if not a CIA // doesn't make sense if not a CIA
if (lp.Roms[i].Extension.ToLowerInvariant() != ".cia") if (".cia".EqualsIgnoreCase(lp.Roms[i].Extension))
{ {
Dispose(); Dispose();
throw new("ROMs after the index 0 should be CIAs"); throw new("ROMs after the index 0 should be CIAs");

View File

@ -35,7 +35,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
public void SetCpuRegister(string register, int value) public void SetCpuRegister(string register, int value)
{ {
if (register.Length == 9 && register.Substring(4, 5).ToUpperInvariant() == " BANK") if (register.Length is 9 && register.EndsWith(" BANK", StringComparison.OrdinalIgnoreCase))
{ {
var type = (LibGambatte.BankType)Enum.Parse(typeof(LibGambatte.BankType), register.Substring(0, 4).ToUpperInvariant()); var type = (LibGambatte.BankType)Enum.Parse(typeof(LibGambatte.BankType), register.Substring(0, 4).ToUpperInvariant());
LibGambatte.gambatte_setbank(GambatteState, type, value); LibGambatte.gambatte_setbank(GambatteState, type, value);

View File

@ -4,6 +4,7 @@ using System.IO;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.PathExtensions; using BizHawk.Common.PathExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Components.W65816; using BizHawk.Emulation.Cores.Components.W65816;
@ -279,7 +280,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES
// every rom requests msu1.rom... why? who knows. // every rom requests msu1.rom... why? who knows.
// also handle msu-1 pcm files here // also handle msu-1 pcm files here
bool isMsu1Rom = hint == "msu1.rom"; bool isMsu1Rom = hint == "msu1.rom";
bool isMsu1Pcm = Path.GetExtension(hint).ToLowerInvariant() == ".pcm"; var isMsu1Pcm = ".pcm".EqualsIgnoreCase(Path.GetExtension(hint));
if (isMsu1Rom || isMsu1Pcm) if (isMsu1Rom || isMsu1Pcm)
{ {
// well, check if we have an msu-1 xml // well, check if we have an msu-1 xml

View File

@ -204,7 +204,7 @@ namespace BizHawk.Emulation.DiscSystem
var parts = line.Split('='); var parts = line.Split('=');
if (parts.Length != 2) if (parts.Length != 2)
throw new CCDParseException("Malformed or unexpected CCD format: parsing item into two parts"); throw new CCDParseException("Malformed or unexpected CCD format: parsing item into two parts");
if (parts[0].ToUpperInvariant() == "FLAGS") if ("FLAGS".EqualsIgnoreCase(parts[0]))
{ {
// flags are a space-separated collection of symbolic constants: // flags are a space-separated collection of symbolic constants:
// https://www.gnu.org/software/ccd2cue/manual/html_node/FLAGS-_0028Compact-Disc-fields_0029.html#FLAGS-_0028Compact-Disc-fields_0029 // https://www.gnu.org/software/ccd2cue/manual/html_node/FLAGS-_0028Compact-Disc-fields_0029.html#FLAGS-_0028Compact-Disc-fields_0029

View File

@ -238,7 +238,7 @@ namespace BizHawk.Emulation.DiscSystem
return DiscType.Playdia; return DiscType.Playdia;
if (sysId is "CDTV" or "AMIGA" if (sysId is "CDTV" or "AMIGA"
|| iso.Root.Children.Keys.Any(k => k.ToLowerInvariant().Contains("cd32"))) || iso.Root.Children.Keys.Any(static k => k.ContainsIgnoreCase("cd32")))
{ {
return DiscType.Amiga; return DiscType.Amiga;
} }

View File

@ -22,7 +22,9 @@ namespace BizHawk.Tests.Emulation.Common
public void CheckFormatOfHashes() public void CheckFormatOfHashes()
{ {
static void CustomAssert(string hash) static void CustomAssert(string hash)
#pragma warning disable CA1862 // testing whether it's all-caps
=> Assert.IsTrue(hash.Length == 40 && hash == hash.ToUpperInvariant() && hash.IsHex(), $"incorrectly formatted: {hash}"); => Assert.IsTrue(hash.Length == 40 && hash == hash.ToUpperInvariant() && hash.IsHex(), $"incorrectly formatted: {hash}");
#pragma warning restore CA1862
foreach (var hash in FirmwareDatabase.FirmwareFilesByHash.Keys) CustomAssert(hash); foreach (var hash in FirmwareDatabase.FirmwareFilesByHash.Keys) CustomAssert(hash);
foreach (var fo in FirmwareDatabase.FirmwareOptions) CustomAssert(fo.Hash); foreach (var fo in FirmwareDatabase.FirmwareOptions) CustomAssert(fo.Hash);
} }