Enable CA2208 and MA0043 and fix noncompliance

"Instantiate argument exceptions correctly" and
"Use nameof operator in ArgumentException"
This commit is contained in:
YoshiRulz 2022-07-20 00:58:03 +10:00
parent 5a8a24e936
commit 92c4714be1
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
25 changed files with 56 additions and 141 deletions

View File

@ -237,7 +237,7 @@
<Rule Id="MA0042" Action="Error" /> <Rule Id="MA0042" Action="Error" />
<!-- Use nameof operator in ArgumentException --> <!-- Use nameof operator in ArgumentException -->
<Rule Id="MA0043" Action="Hidden" /> <Rule Id="MA0043" Action="Error" />
<!-- Remove useless ToString call --> <!-- Remove useless ToString call -->
<Rule Id="MA0044" Action="Hidden" /> <Rule Id="MA0044" Action="Hidden" />
@ -622,7 +622,7 @@
<Rule Id="CA2101" Action="Hidden" /> <Rule Id="CA2101" Action="Hidden" />
<!-- Instantiate argument exceptions correctly --> <!-- Instantiate argument exceptions correctly -->
<Rule Id="CA2208" Action="Hidden" /> <Rule Id="CA2208" Action="Error" />
<!-- Non-constant fields should not be visible --> <!-- Non-constant fields should not be visible -->
<Rule Id="CA2211" Action="Hidden" /> <Rule Id="CA2211" Action="Hidden" />

View File

@ -38,7 +38,7 @@ namespace BizHawk.BizInvoke
set set
{ {
if (value < 0 || value > _length) if (value < 0 || value > _length)
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException(paramName: nameof(value), value, message: "index out of range");
_pos = value; _pos = value;
} }
} }

View File

@ -664,9 +664,7 @@ namespace BizHawk.Bizware.OpenTK3
private GLControl CastControl(swf.Control swfControl) private GLControl CastControl(swf.Control swfControl)
{ {
GLControl glc = swfControl as GLControl; if (swfControl is not GLControl glc) throw new ArgumentException(message: "Argument isn't a control created by the IGL interface", paramName: nameof(swfControl));
if (glc == null)
throw new ArgumentException("Argument isn't a control created by the IGL interface", "glControl");
return glc; return glc;
} }

View File

@ -32,9 +32,9 @@ namespace BizHawk.Client.Common
_dialogParent = dialogParent; _dialogParent = dialogParent;
_quickBmpFile = quickBmpFile; _quickBmpFile = quickBmpFile;
_pauseCallback = pauseCallback _pauseCallback = pauseCallback
?? throw new ArgumentNullException($"{nameof(pauseCallback)} cannot be null."); ?? throw new ArgumentNullException(paramName: nameof(pauseCallback));
_modeChangedCallback = modeChangedCallback _modeChangedCallback = modeChangedCallback
?? throw new ArgumentNullException($"{nameof(modeChangedCallback)} CannotUnloadAppDomainException be null."); ?? throw new ArgumentNullException(paramName: nameof(modeChangedCallback));
} }
public IMovieConfig Settings { get; } public IMovieConfig Settings { get; }

View File

@ -14,7 +14,9 @@ namespace BizHawk.Client.Common
{ {
if (string.IsNullOrWhiteSpace(filename)) if (string.IsNullOrWhiteSpace(filename))
{ {
throw new ArgumentNullException($"{nameof(filename)} can not be null."); throw filename is null
? new ArgumentNullException(paramName: nameof(filename))
: new ArgumentException(message: "path cannot be blank", paramName: nameof(filename));
} }
Session = session; Session = session;

View File

@ -105,10 +105,7 @@ namespace BizHawk.Client.Common
public new void Add(TasBranch item) public new void Add(TasBranch item)
{ {
if (item == null) if (item is null) throw new ArgumentNullException(paramName: nameof(item));
{
throw new ArgumentNullException($"{nameof(item)} cannot be null");
}
if (item.Uuid == Guid.Empty) if (item.Uuid == Guid.Empty)
{ {

View File

@ -24,10 +24,7 @@ namespace BizHawk.Client.Common
throw new ArgumentException("ZwinderBuffer's settings cannot be null."); throw new ArgumentException("ZwinderBuffer's settings cannot be null.");
long targetSize = settings.BufferSize * 1024 * 1024; long targetSize = settings.BufferSize * 1024 * 1024;
if (settings.TargetFrameLength < 1) if (settings.TargetFrameLength < 1) throw new ArgumentException(message: nameof(IRewindSettings.TargetFrameLength) + " of provided settings is invalid", paramName: nameof(settings));
{
throw new ArgumentOutOfRangeException(nameof(settings.TargetFrameLength));
}
Size = 1L << (int)Math.Floor(Math.Log(targetSize, 2)); Size = 1L << (int)Math.Floor(Math.Log(targetSize, 2));
_sizeMask = Size - 1; _sizeMask = Size - 1;

View File

@ -112,10 +112,7 @@ namespace BizHawk.Client.Common
/// <exception cref="ArgumentNullException"><paramref name="cheat"/> is null</exception> /// <exception cref="ArgumentNullException"><paramref name="cheat"/> is null</exception>
public void Add(Cheat cheat) public void Add(Cheat cheat)
{ {
if (cheat is null) if (cheat is null) throw new ArgumentNullException(paramName: nameof(cheat));
{
throw new ArgumentNullException($"{nameof(cheat)} can not be null");
}
if (cheat.IsSeparator) if (cheat.IsSeparator)
{ {

View File

@ -73,11 +73,7 @@ namespace BizHawk.Client.EmuHawk
/// <exception cref="ArgumentException"><paramref name="buffer"/> is not in use</exception> /// <exception cref="ArgumentException"><paramref name="buffer"/> is not in use</exception>
public void ReleaseBuffer(T[] buffer) public void ReleaseBuffer(T[] buffer)
{ {
if (!_inUse.Remove(buffer)) if (!_inUse.Remove(buffer)) throw new ArgumentException(message: "already released?", paramName: nameof(buffer));
{
throw new ArgumentException();
}
_available.Add(buffer); _available.Add(buffer);
} }
} }

View File

@ -11,10 +11,7 @@ namespace BizHawk.Client.EmuHawk
{ {
public bool StartNewMovie(IMovie movie, bool record) public bool StartNewMovie(IMovie movie, bool record)
{ {
if (movie == null) if (movie is null) throw new ArgumentNullException(paramName: nameof(movie));
{
throw new ArgumentNullException($"{nameof(movie)} cannot be null.");
}
var oldPreferredCores = new Dictionary<string, string>(Config.PreferredCores); var oldPreferredCores = new Dictionary<string, string>(Config.PreferredCores);
try try

View File

@ -104,7 +104,7 @@ namespace BizHawk.Client.EmuHawk
NLuaRadio.Checked = true; NLuaRadio.Checked = true;
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new InvalidOperationException();
} }
} }

View File

@ -104,13 +104,13 @@ namespace BizHawk.Common
/// <exception cref="ArgumentException"><paramref name="str"/> has an odd number of chars or contains a char not in <c>[0-9A-Fa-f]</c></exception> /// <exception cref="ArgumentException"><paramref name="str"/> has an odd number of chars or contains a char not in <c>[0-9A-Fa-f]</c></exception>
public static byte[] HexStringToBytes(this string str) public static byte[] HexStringToBytes(this string str)
{ {
if (str.Length % 2 != 0) throw new ArgumentException(); if (str.Length % 2 is not 0) throw new ArgumentException(message: "string length must be even (add 0 padding if necessary)", paramName: nameof(str));
static int CharToNybble(char c) static int CharToNybble(char c)
{ {
if ('0' <= c && c <= '9') return c - 0x30; if ('0' <= c && c <= '9') return c - 0x30;
if ('A' <= c && c <= 'F') return c - 0x37; if ('A' <= c && c <= 'F') return c - 0x37;
if ('a' <= c && c <= 'f') return c - 0x57; if ('a' <= c && c <= 'f') return c - 0x57;
throw new ArgumentException(); throw new ArgumentException(message: "not a hex digit", paramName: nameof(c));
} }
using var ms = new MemoryStream(); using var ms = new MemoryStream();
for (int i = 0, l = str.Length / 2; i != l; i++) ms.WriteByte((byte) ((CharToNybble(str[2 * i]) << 4) + CharToNybble(str[2 * i + 1]))); for (int i = 0, l = str.Length / 2; i != l; i++) ms.WriteByte((byte) ((CharToNybble(str[2 * i]) << 4) + CharToNybble(str[2 * i + 1])));

View File

@ -51,7 +51,7 @@ namespace BizHawk.Common
private static void gf2_matrix_square(Span<uint> square, ReadOnlySpan<uint> mat) private static void gf2_matrix_square(Span<uint> square, ReadOnlySpan<uint> mat)
{ {
if (mat.Length != square.Length) throw new ArgumentException(); if (mat.Length != square.Length) throw new ArgumentException(message: "must be same length as " + nameof(square), paramName: nameof(mat));
for (var n = 0; n < square.Length; n++) square[n] = gf2_matrix_times(mat, mat[n]); for (var n = 0; n < square.Length; n++) square[n] = gf2_matrix_times(mat, mat[n]);
} }

View File

@ -105,10 +105,8 @@ namespace BizHawk.Emulation.Common
public virtual void BulkPeekByte(Range<long> addresses, byte[] values) public virtual void BulkPeekByte(Range<long> addresses, byte[] values)
{ {
if (addresses == null || values == null) if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
{ if (values is null) throw new ArgumentNullException(paramName: nameof(values));
throw new ArgumentException();
}
if ((long) addresses.Count() != values.Length) if ((long) addresses.Count() != values.Length)
{ {
@ -126,10 +124,9 @@ namespace BizHawk.Emulation.Common
public virtual void BulkPeekUshort(Range<long> addresses, bool bigEndian, ushort[] values) public virtual void BulkPeekUshort(Range<long> addresses, bool bigEndian, ushort[] values)
{ {
if (addresses == null || values == null) if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
{ if (values is null) throw new ArgumentNullException(paramName: nameof(values));
throw new ArgumentException();
}
var start = addresses.Start; var start = addresses.Start;
var end = addresses.EndInclusive + 1; var end = addresses.EndInclusive + 1;
@ -151,10 +148,9 @@ namespace BizHawk.Emulation.Common
public virtual void BulkPeekUint(Range<long> addresses, bool bigEndian, uint[] values) public virtual void BulkPeekUint(Range<long> addresses, bool bigEndian, uint[] values)
{ {
if (addresses == null || values == null) if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
{ if (values is null) throw new ArgumentNullException(paramName: nameof(values));
throw new ArgumentException();
}
var start = addresses.Start; var start = addresses.Start;
var end = addresses.EndInclusive + 1; var end = addresses.EndInclusive + 1;

View File

@ -90,7 +90,7 @@ namespace BizHawk.Emulation.Common
Position = Length + offset; Position = Length + offset;
break; break;
default: default:
throw new ArgumentOutOfRangeException("origin"); throw new ArgumentOutOfRangeException(paramName: nameof(origin));
} }
return Position; return Position;
} }

View File

@ -46,11 +46,7 @@ namespace BizHawk.Emulation.Common
get => _cpu ??= AvailableCpus.First(); get => _cpu ??= AvailableCpus.First();
set set
{ {
if (!AvailableCpus.Contains(value)) if (!AvailableCpus.Contains(value)) throw new ArgumentException(message: $"must be the name of a CPU with disassembly available (see {nameof(AvailableCpus)})", paramName: nameof(value));
{
throw new ArgumentException();
}
_cpu = value; _cpu = value;
} }
} }

View File

@ -143,36 +143,24 @@ namespace BizHawk.Emulation.Common
return BlipBufDll.blip_samples_avail(_context); return BlipBufDll.blip_samples_avail(_context);
} }
/// <exception cref="ArgumentOutOfRangeException"><paramref name="output"/> can't hold <paramref name="count"/> samples (or twice that if <paramref name="stereo"/> is <see langword="true"/>)</exception> /// <exception cref="ArgumentException"><paramref name="output"/> can't hold <paramref name="count"/> samples (or twice that if <paramref name="stereo"/> is <see langword="true"/>)</exception>
public int ReadSamples(short[] output, int count, bool stereo) public int ReadSamples(short[] output, int count, bool stereo)
{ {
if (output.Length < count * (stereo ? 2 : 1)) if (output.Length < count * (stereo ? 2 : 1)) throw new ArgumentException(message: "buffer too small", paramName: nameof(output));
{
throw new ArgumentOutOfRangeException();
}
return BlipBufDll.blip_read_samples(_context, output, count, stereo ? 1 : 0); return BlipBufDll.blip_read_samples(_context, output, count, stereo ? 1 : 0);
} }
/// <exception cref="ArgumentOutOfRangeException"><paramref name="output"/> can't hold 2 * <paramref name="count"/> samples</exception> /// <exception cref="ArgumentException"><paramref name="output"/> can't hold 2 * <paramref name="count"/> samples</exception>
public int ReadSamplesLeft(short[] output, int count) public int ReadSamplesLeft(short[] output, int count)
{ {
if (output.Length < count * 2) if (output.Length < count * 2) throw new ArgumentException(message: "buffer too small", paramName: nameof(output));
{
throw new ArgumentOutOfRangeException();
}
return BlipBufDll.blip_read_samples(_context, output, count, 1); return BlipBufDll.blip_read_samples(_context, output, count, 1);
} }
/// <exception cref="ArgumentOutOfRangeException"><paramref name="output"/> can't hold 2 * <paramref name="count"/> samples</exception> /// <exception cref="ArgumentException"><paramref name="output"/> can't hold 2 * <paramref name="count"/> samples</exception>
public int ReadSamplesRight(short[] output, int count) public int ReadSamplesRight(short[] output, int count)
{ {
if (output.Length < count * 2) if (output.Length < count * 2) throw new ArgumentException(message: "buffer too small", paramName: nameof(output));
{
throw new ArgumentOutOfRangeException();
}
unsafe unsafe
{ {
fixed (short* s = &output[1]) fixed (short* s = &output[1])

View File

@ -33,15 +33,8 @@ namespace BizHawk.Emulation.Common
/// <exception cref="ArgumentOutOfRangeException"><paramref name="filterWidth"/> is not in 8..65536</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="filterWidth"/> is not in 8..65536</exception>
public DCFilter(ISoundProvider input, int filterWidth) public DCFilter(ISoundProvider input, int filterWidth)
{ {
if (input == null) if (input is null) throw new ArgumentNullException(paramName: nameof(input));
{ if (filterWidth is < 8 or > 65536) throw new ArgumentOutOfRangeException(paramName: nameof(filterWidth), filterWidth, message: "invalid width");
throw new ArgumentNullException();
}
if (filterWidth < 8 || filterWidth > 65536)
{
throw new ArgumentOutOfRangeException();
}
_depth = DepthFromFilterWidth(filterWidth); _depth = DepthFromFilterWidth(filterWidth);

View File

@ -11,10 +11,7 @@ namespace BizHawk.Emulation.Cores.Arcades.MAME
private byte _peek(long addr, int firstOffset, long size) private byte _peek(long addr, int firstOffset, long size)
{ {
if (addr < 0 || addr >= size) if (addr < 0 || addr >= size) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
{
throw new ArgumentOutOfRangeException();
}
if (!_memAccess) if (!_memAccess)
{ {
@ -33,10 +30,7 @@ namespace BizHawk.Emulation.Cores.Arcades.MAME
private void _poke(long addr, byte val, int firstOffset, long size) private void _poke(long addr, byte val, int firstOffset, long size)
{ {
if (addr < 0 || addr >= size) if (addr < 0 || addr >= size) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
{
throw new ArgumentOutOfRangeException();
}
if (!_memAccess) if (!_memAccess)
{ {

View File

@ -31,7 +31,11 @@ namespace BizHawk.Emulation.Cores.Components.Z80A
public int InterruptMode public int InterruptMode
{ {
get => interruptMode; get => interruptMode;
set { if (value < 0 || value > 2) throw new ArgumentOutOfRangeException(); interruptMode = value; } set
{
if (value is < 0 or > 2) throw new ArgumentOutOfRangeException(paramName: nameof(value), value, message: "invalid interrupt mode");
interruptMode = value;
}
} }
public Action IRQCallback = () => {}; public Action IRQCallback = () => {};

View File

@ -26,10 +26,7 @@ namespace BizHawk.Emulation.Cores.Atari.Lynx
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
if (size != srcData.Length) if (srcData.Length != size) throw new ArgumentException(message: "buffer too small", paramName: nameof(srcData));
{
throw new ArgumentOutOfRangeException();
}
Marshal.Copy(srcData, 0, data, size); Marshal.Copy(srcData, 0, data, size);
} }

View File

@ -41,21 +41,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
addr => addr =>
{ {
var a = (uint)addr; var a = (uint)addr;
if (a >= 0x10000000) if (a >= 0x10000000) throw new ArgumentOutOfRangeException(paramName: nameof(addr), a, message: "address out of range");
{
throw new ArgumentOutOfRangeException();
}
return LibmGBA.BizReadBus(Core, a); return LibmGBA.BizReadBus(Core, a);
}, },
(addr, val) => (addr, val) =>
{ {
var a = (uint)addr; var a = (uint)addr;
if (a >= 0x10000000) if (a >= 0x10000000) throw new ArgumentOutOfRangeException(paramName: nameof(addr), a, message: "address out of range");
{
throw new ArgumentOutOfRangeException();
}
LibmGBA.BizWriteBus(Core, a, val); LibmGBA.BizWriteBus(Core, a, val);
}, 4)); }, 4));

View File

@ -32,20 +32,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64
{ {
peekByte = addr => peekByte = addr =>
{ {
if (addr < 0 || addr >= size) if (addr < 0 || addr >= size) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
{
throw new ArgumentOutOfRangeException();
}
return Marshal.ReadByte(memPtr, (int)(addr ^ 3)); return Marshal.ReadByte(memPtr, (int)(addr ^ 3));
}; };
pokeByte = (addr, val) => pokeByte = (addr, val) =>
{ {
if (addr < 0 || addr >= size) if (addr < 0 || addr >= size) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
{
throw new ArgumentOutOfRangeException();
}
Marshal.WriteByte(memPtr, (int)(addr ^ 3), val); Marshal.WriteByte(memPtr, (int)(addr ^ 3), val);
}; };
} }
@ -53,20 +45,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64
{ {
peekByte = addr => peekByte = addr =>
{ {
if (addr < 0 || addr >= size) if (addr < 0 || addr >= size) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
{
throw new ArgumentOutOfRangeException();
}
return Marshal.ReadByte(memPtr, (int)(addr)); return Marshal.ReadByte(memPtr, (int)(addr));
}; };
pokeByte = (addr, val) => pokeByte = (addr, val) =>
{ {
if (addr < 0 || addr >= size) if (addr < 0 || addr >= size) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
{
throw new ArgumentOutOfRangeException();
}
Marshal.WriteByte(memPtr, (int)(addr), val); Marshal.WriteByte(memPtr, (int)(addr), val);
}; };
} }

View File

@ -153,8 +153,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
public void InsertSide(int side) public void InsertSide(int side)
{ {
if (side >= NumSides) if (side >= NumSides) throw new ArgumentOutOfRangeException(paramName: nameof(side), side, message: "index out of range");
throw new ArgumentOutOfRangeException();
byte[] buf = new byte[65500]; byte[] buf = new byte[65500];
Buffer.BlockCopy(diskimage, 16 + side * 65500, buf, 0, 65500); Buffer.BlockCopy(diskimage, 16 + side * 65500, buf, 0, 65500);
diskdrive.InsertBrokenImage(buf, false /*true*/); diskdrive.InsertBrokenImage(buf, false /*true*/);

View File

@ -80,19 +80,10 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES
get => _Palette; get => _Palette;
set set
{ {
if (value == null) if (value is null) throw new ArgumentNullException(paramName: nameof(value));
{ const int PALETTE_LENGTH = 64 * 8 * 3;
throw new ArgumentNullException(); if (value.Length is not PALETTE_LENGTH) throw new ArgumentException(message: "incorrect length", paramName: nameof(value));
} _Palette = value;
if (value.Length == 64 * 8 * 3)
{
_Palette = value;
}
else
{
throw new ArgumentOutOfRangeException();
}
} }
} }
@ -117,10 +108,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES
//TODO - support 512 color palettes //TODO - support 512 color palettes
int nColors = pal.GetLength(0); int nColors = pal.GetLength(0);
int nElems = pal.GetLength(1); int nElems = pal.GetLength(1);
if (!(nColors == 64 || nColors == 512) || nElems != 3) if (nColors is not (64 or 512) || nElems is not 3) throw new ArgumentException(message: "incorrect array dimensions", paramName: nameof(pal));
{
throw new ArgumentOutOfRangeException();
}
if (nColors == 512) if (nColors == 512)
{ {