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

View File

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

View File

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

View File

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

View File

@ -14,7 +14,9 @@ namespace BizHawk.Client.Common
{
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;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -104,7 +104,7 @@ namespace BizHawk.Client.EmuHawk
NLuaRadio.Checked = true;
break;
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>
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)
{
if ('0' <= c && c <= '9') return c - 0x30;
if ('A' <= c && c <= 'F') return c - 0x37;
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();
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)
{
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]);
}

View File

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

View File

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

View File

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

View File

@ -143,36 +143,24 @@ namespace BizHawk.Emulation.Common
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)
{
if (output.Length < count * (stereo ? 2 : 1))
{
throw new ArgumentOutOfRangeException();
}
if (output.Length < count * (stereo ? 2 : 1)) throw new ArgumentException(message: "buffer too small", paramName: nameof(output));
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)
{
if (output.Length < count * 2)
{
throw new ArgumentOutOfRangeException();
}
if (output.Length < count * 2) throw new ArgumentException(message: "buffer too small", paramName: nameof(output));
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)
{
if (output.Length < count * 2)
{
throw new ArgumentOutOfRangeException();
}
if (output.Length < count * 2) throw new ArgumentException(message: "buffer too small", paramName: nameof(output));
unsafe
{
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>
public DCFilter(ISoundProvider input, int filterWidth)
{
if (input == null)
{
throw new ArgumentNullException();
}
if (filterWidth < 8 || filterWidth > 65536)
{
throw new ArgumentOutOfRangeException();
}
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");
_depth = DepthFromFilterWidth(filterWidth);

View File

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

View File

@ -31,7 +31,11 @@ namespace BizHawk.Emulation.Cores.Components.Z80A
public int 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 = () => {};

View File

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

View File

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

View File

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

View File

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

View File

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