ThrowIfNull net7

This commit is contained in:
Kurt 2023-02-23 01:07:46 -08:00
parent 552676ed3a
commit 812aa0a2d3
9 changed files with 21 additions and 39 deletions

View file

@ -31,8 +31,7 @@ public abstract class DataMysteryGift : MysteryGift
{
byte[] data = (byte[])Data.Clone();
var result = GetMysteryGift(data);
if (result == null)
throw new ArgumentException(nameof(MysteryGift));
ArgumentNullException.ThrowIfNull(result);
return result;
}

View file

@ -16,9 +16,12 @@ public static class EntityBlank
/// <returns>New instance of a blank <see cref="PKM"/> object.</returns>
public static PKM GetBlank(Type type)
{
// Not all derived types have a parameter-less constructor, so find the minimal constructor and use that.
var constructors = type.GetTypeInfo().DeclaredConstructors.Where(z => !z.IsStatic);
var argCount = constructors.Min(z => z.GetParameters().Length);
return (PKM)(Activator.CreateInstance(type, new object[argCount]) ?? throw new ArgumentException(null, nameof(type)));
var pk = Activator.CreateInstance(type, new object[argCount]) as PKM;
ArgumentNullException.ThrowIfNull(pk);
return pk;
}
public static PKM GetBlank(int gen, GameVersion ver) => gen switch

View file

@ -42,8 +42,7 @@ public sealed class BitmapAnimator : IDisposable
}
// reset logic
if (GlowCache == null)
throw new ArgumentNullException(nameof(GlowCache));
ArgumentNullException.ThrowIfNull(GlowCache);
GlowCounter = 0;
for (int i = 0; i < GlowCache.Length; i++)
GlowCache[i] = null;
@ -90,8 +89,7 @@ public sealed class BitmapAnimator : IDisposable
private Image GetFrame(int frameIndex)
{
var cache = GlowCache;
if (cache == null)
throw new NullReferenceException(nameof(GlowCache));
ArgumentNullException.ThrowIfNull(cache);
var frame = cache[frameIndex];
if (frame != null)
return frame;
@ -99,8 +97,7 @@ public sealed class BitmapAnimator : IDisposable
var elapsedFraction = (double)frameIndex / GlowInterval;
var frameColor = GetFrameColor(elapsedFraction);
if (GlowData == null)
throw new NullReferenceException(nameof(GlowData));
ArgumentNullException.ThrowIfNull(GlowData);
var frameData = (byte[])GlowData.Clone();
ImageUtil.ChangeAllColorTo(frameData, frameColor);

View file

@ -334,11 +334,7 @@ public partial class SAV_Encounters : Form
return RuntimeHelpers.GetHashCode(x).Equals(RuntimeHelpers.GetHashCode(y));
}
public int GetHashCode(T obj)
{
if (obj == null) throw new ArgumentNullException(nameof(obj));
return RuntimeHelpers.GetHashCode(obj);
}
public int GetHashCode(T obj) => RuntimeHelpers.GetHashCode(obj);
}
private IEnumerable<IEncounterInfo> GetEncounters(ushort species, byte form, ushort[] moves, PKM pk, IReadOnlyList<GameVersion> vers)

View file

@ -287,8 +287,7 @@ public partial class SAV_Pokedex4 : Form
}
var item = lb.SelectedItem;
if (item is null)
throw new NullReferenceException();
ArgumentNullException.ThrowIfNull(item);
lb.Items.RemoveAt(lb.SelectedIndex);
var dest = lb == LB_Gender ? LB_NGender : LB_Gender;
dest.Items.Add(item);
@ -319,8 +318,7 @@ public partial class SAV_Pokedex4 : Form
return;
var item = lb.SelectedItem;
if (item is null)
throw new NullReferenceException();
ArgumentNullException.ThrowIfNull(item);
lb.Items.Remove(item);
lb.Items.Insert(newIndex, item);
lb.SelectedIndex = newIndex;
@ -338,8 +336,7 @@ public partial class SAV_Pokedex4 : Form
}
var item = lb.SelectedItem;
if (item is null)
throw new NullReferenceException();
ArgumentNullException.ThrowIfNull(item);
lb.Items.RemoveAt(lb.SelectedIndex);
var dest = lb == LB_Form ? LB_NForm : LB_Form;
dest.Items.Add(item);
@ -370,8 +367,7 @@ public partial class SAV_Pokedex4 : Form
return;
var item = lb.SelectedItem;
if (item is null)
throw new NullReferenceException();
ArgumentNullException.ThrowIfNull(item);
lb.Items.Remove(item);
lb.Items.Insert(newIndex, item);
lb.SelectedIndex = newIndex;

View file

@ -396,12 +396,11 @@ public partial class SAV_SecretBase : Form
var data = File.ReadAllBytes(path);
var obj = SecretBase6.Read(data);
if (obj is null) // shouldn't happen, we already size-check above.
throw new NullReferenceException();
// shouldn't happen, we already size-check above.
ArgumentNullException.ThrowIfNull(obj);
var sb = CurrentBase;
if (sb is null)
throw new NullReferenceException();
ArgumentNullException.ThrowIfNull(sb);
ResetLoadNew();
sb.Load(obj);
@ -413,8 +412,7 @@ public partial class SAV_SecretBase : Form
private void B_Export_Click(object sender, EventArgs e)
{
var sb = CurrentBase;
if (sb is null)
throw new NullReferenceException();
ArgumentNullException.ThrowIfNull(sb);
SaveCurrent(sb);
var tr = sb.TrainerName;
@ -441,8 +439,7 @@ public partial class SAV_SecretBase : Form
int index = LB_Bases.SelectedIndex - 1;
var bdata = CurrentBase;
if (bdata is null)
throw new NullReferenceException();
ArgumentNullException.ThrowIfNull(bdata);
string BaseTrainer = bdata.TrainerName;
if (string.IsNullOrEmpty(BaseTrainer))

View file

@ -362,9 +362,6 @@ public partial class SAV_Inventory : Form
private void ModifyPouch(int pouch, Action<InventoryPouch> func)
{
if (func == null)
throw new ArgumentNullException(nameof(func));
var dgv = GetGrid(pouch);
var p = Pouches[pouch];
SetBag(dgv, p); // save current

View file

@ -363,9 +363,7 @@ public static class WinFormsUtil
sav.CurrentBox = currentBox;
var path = sfd.FileName;
if (path == null)
throw new NullReferenceException(nameof(sfd.FileName));
ArgumentNullException.ThrowIfNull(path);
ExportSAV(sav, path);
return true;
}

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
namespace PKHeX.Core.Tests;
@ -11,8 +11,7 @@ internal static class TestUtil
while (!folder.EndsWith(nameof(Tests)))
{
var dir = Directory.GetParent(folder);
if (dir == null)
throw new ArgumentNullException(nameof(dir));
ArgumentNullException.ThrowIfNull(dir);
folder = dir.FullName;
}
return folder;