Change throw exceptions to use nameof in parameter

This commit is contained in:
teinarss
2021-02-28 17:35:36 +01:00
committed by abcdefg30
parent 53b781960c
commit ed295ae315
30 changed files with 46 additions and 46 deletions

View File

@@ -159,7 +159,7 @@ namespace OpenRA
if (xs.Count == 0) if (xs.Count == 0)
{ {
if (throws) if (throws)
throw new ArgumentException("Collection must not be empty.", "ts"); throw new ArgumentException("Collection must not be empty.", nameof(ts));
else else
return default(T); return default(T);
} }
@@ -236,7 +236,7 @@ namespace OpenRA
{ {
if (!e.MoveNext()) if (!e.MoveNext())
if (throws) if (throws)
throw new ArgumentException("Collection must not be empty.", "ts"); throw new ArgumentException("Collection must not be empty.", nameof(ts));
else else
return default(T); return default(T);
t = e.Current; t = e.Current;

View File

@@ -28,7 +28,7 @@ namespace OpenRA.FileFormats
public ReplayMetadata(GameInformation info) public ReplayMetadata(GameInformation info)
{ {
if (info == null) if (info == null)
throw new ArgumentNullException("info"); throw new ArgumentNullException(nameof(info));
GameInfo = info; GameInfo = info;
} }

View File

@@ -97,10 +97,10 @@ namespace OpenRA
public void AddPlayer(OpenRA.Player runtimePlayer, Session lobbyInfo) public void AddPlayer(OpenRA.Player runtimePlayer, Session lobbyInfo)
{ {
if (runtimePlayer == null) if (runtimePlayer == null)
throw new ArgumentNullException("runtimePlayer"); throw new ArgumentNullException(nameof(runtimePlayer));
if (lobbyInfo == null) if (lobbyInfo == null)
throw new ArgumentNullException("lobbyInfo"); throw new ArgumentNullException(nameof(lobbyInfo));
// We don't care about spectators and map players // We don't care about spectators and map players
if (runtimePlayer.NonCombatant || !runtimePlayer.Playable) if (runtimePlayer.NonCombatant || !runtimePlayer.Playable)

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Graphics
public SpriteFont(string name, byte[] data, int size, int ascender, float scale, SheetBuilder builder) public SpriteFont(string name, byte[] data, int size, int ascender, float scale, SheetBuilder builder)
{ {
if (builder.Type != SheetType.BGRA) if (builder.Type != SheetType.BGRA)
throw new ArgumentException("The sheet builder must create BGRA sheets.", "builder"); throw new ArgumentException("The sheet builder must create BGRA sheets.", nameof(builder));
deviceScale = scale; deviceScale = scale;
this.size = size; this.size = size;

View File

@@ -38,7 +38,7 @@ namespace OpenRA
public virtual void CopyValuesFrom(CellLayerBase<T> anotherLayer) public virtual void CopyValuesFrom(CellLayerBase<T> anotherLayer)
{ {
if (Size != anotherLayer.Size || GridType != anotherLayer.GridType) if (Size != anotherLayer.Size || GridType != anotherLayer.GridType)
throw new ArgumentException("Layers must have a matching size and shape (grid type).", "anotherLayer"); throw new ArgumentException("Layers must have a matching size and shape (grid type).", nameof(anotherLayer));
Array.Copy(anotherLayer.entries, entries, entries.Length); Array.Copy(anotherLayer.entries, entries, entries.Length);
} }

View File

@@ -62,7 +62,7 @@ namespace OpenRA
public static CellRegion BoundingRegion(MapGridType shape, IEnumerable<CPos> cells) public static CellRegion BoundingRegion(MapGridType shape, IEnumerable<CPos> cells)
{ {
if (cells == null || !cells.Any()) if (cells == null || !cells.Any())
throw new ArgumentException("cells must not be null or empty.", "cells"); throw new ArgumentException("cells must not be null or empty.", nameof(cells));
var minU = int.MaxValue; var minU = int.MaxValue;
var minV = int.MaxValue; var minV = int.MaxValue;

View File

@@ -1246,10 +1246,10 @@ namespace OpenRA
public IEnumerable<CPos> FindTilesInAnnulus(CPos center, int minRange, int maxRange, bool allowOutsideBounds = false) public IEnumerable<CPos> FindTilesInAnnulus(CPos center, int minRange, int maxRange, bool allowOutsideBounds = false)
{ {
if (maxRange < minRange) if (maxRange < minRange)
throw new ArgumentOutOfRangeException("maxRange", "Maximum range is less than the minimum range."); throw new ArgumentOutOfRangeException(nameof(maxRange), "Maximum range is less than the minimum range.");
if (maxRange >= Grid.TilesByDistance.Length) if (maxRange >= Grid.TilesByDistance.Length)
throw new ArgumentOutOfRangeException("maxRange", throw new ArgumentOutOfRangeException(nameof(maxRange),
"The requested range ({0}) cannot exceed the value of MaximumTileSearchRange ({1})".F(maxRange, Grid.MaximumTileSearchRange)); "The requested range ({0}) cannot exceed the value of MaximumTileSearchRange ({1})".F(maxRange, Grid.MaximumTileSearchRange));
for (var i = minRange; i <= maxRange; i++) for (var i = minRange; i <= maxRange; i++)

View File

@@ -296,7 +296,7 @@ namespace OpenRA.Network
case 1: return item2OrSentinel; case 1: return item2OrSentinel;
case 2: return item3; case 2: return item3;
case 3: return item4; case 3: return item4;
default: throw new ArgumentOutOfRangeException("index"); default: throw new ArgumentOutOfRangeException(nameof(index));
} }
} }
@@ -314,7 +314,7 @@ namespace OpenRA.Network
case 1: item2OrSentinel = value; break; case 1: item2OrSentinel = value; break;
case 2: item3 = value; break; case 2: item3 = value; break;
case 3: item4 = value; break; case 3: item4 = value; break;
default: throw new ArgumentOutOfRangeException("index"); default: throw new ArgumentOutOfRangeException(nameof(index));
} }
} }
} }

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Primitives
public void Add(Action a, long desiredTime) public void Add(Action a, long desiredTime)
{ {
if (a == null) if (a == null)
throw new ArgumentNullException("a"); throw new ArgumentNullException(nameof(a));
lock (actions) lock (actions)
{ {

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Primitives
public Cache(Func<T, U> loader, IEqualityComparer<T> c) public Cache(Func<T, U> loader, IEqualityComparer<T> c)
{ {
if (loader == null) if (loader == null)
throw new ArgumentNullException("loader"); throw new ArgumentNullException(nameof(loader));
this.loader = loader; this.loader = loader;
cache = new Dictionary<T, U>(c); cache = new Dictionary<T, U>(c);

View File

@@ -23,7 +23,7 @@ namespace OpenRA.Primitives
public ConcurrentCache(Func<T, U> loader, IEqualityComparer<T> c) public ConcurrentCache(Func<T, U> loader, IEqualityComparer<T> c)
{ {
if (loader == null) if (loader == null)
throw new ArgumentNullException("loader"); throw new ArgumentNullException(nameof(loader));
this.loader = loader; this.loader = loader;
cache = new ConcurrentDictionary<T, U>(c); cache = new ConcurrentDictionary<T, U>(c);

View File

@@ -28,9 +28,9 @@ namespace OpenRA.Primitives
protected ReadOnlyAdapterStream(Stream stream) protected ReadOnlyAdapterStream(Stream stream)
{ {
if (stream == null) if (stream == null)
throw new ArgumentNullException("stream"); throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead) if (!stream.CanRead)
throw new ArgumentException("stream must be readable.", "stream"); throw new ArgumentException("stream must be readable.", nameof(stream));
baseStream = stream; baseStream = stream;
} }

View File

@@ -57,7 +57,7 @@ namespace OpenRA
public ReadOnlyDictionary(IDictionary<TKey, TValue> dict) public ReadOnlyDictionary(IDictionary<TKey, TValue> dict)
{ {
if (dict == null) if (dict == null)
throw new ArgumentNullException("dict"); throw new ArgumentNullException(nameof(dict));
this.dict = dict; this.dict = dict;
} }

View File

@@ -52,7 +52,7 @@ namespace OpenRA
public ReadOnlyList(IList<T> list) public ReadOnlyList(IList<T> list)
{ {
if (list == null) if (list == null)
throw new ArgumentNullException("list"); throw new ArgumentNullException(nameof(list));
this.list = list; this.list = list;
} }

View File

@@ -33,13 +33,13 @@ namespace OpenRA.Primitives
public SegmentStream(Stream stream, long offset, long count) public SegmentStream(Stream stream, long offset, long count)
{ {
if (stream == null) if (stream == null)
throw new ArgumentNullException("stream"); throw new ArgumentNullException(nameof(stream));
if (!stream.CanSeek) if (!stream.CanSeek)
throw new ArgumentException("stream must be seekable.", "stream"); throw new ArgumentException("stream must be seekable.", nameof(stream));
if (offset < 0) if (offset < 0)
throw new ArgumentOutOfRangeException("offset", "offset must be non-negative."); throw new ArgumentOutOfRangeException(nameof(offset), "offset must be non-negative.");
if (count < 0) if (count < 0)
throw new ArgumentOutOfRangeException("count", "count must be non-negative."); throw new ArgumentOutOfRangeException(nameof(count), "count must be non-negative.");
BaseStream = stream; BaseStream = stream;
BaseOffset = offset; BaseOffset = offset;
@@ -93,7 +93,7 @@ namespace OpenRA.Primitives
{ {
switch (origin) switch (origin)
{ {
default: throw new InvalidEnumArgumentException("origin", (int)origin, typeof(SeekOrigin)); default: throw new InvalidEnumArgumentException(nameof(origin), (int)origin, typeof(SeekOrigin));
case SeekOrigin.Begin: case SeekOrigin.Begin:
return BaseStream.Seek(BaseOffset + offset, SeekOrigin.Begin) - offset; return BaseStream.Seek(BaseOffset + offset, SeekOrigin.Begin) - offset;
case SeekOrigin.Current: case SeekOrigin.Current:

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Primitives
void ValidateBounds(T actor, Rectangle bounds) void ValidateBounds(T actor, Rectangle bounds)
{ {
if (bounds.Width == 0 || bounds.Height == 0) if (bounds.Width == 0 || bounds.Height == 0)
throw new ArgumentException("Bounds of actor {0} are empty.".F(actor), "bounds"); throw new ArgumentException("Bounds of actor {0} are empty.".F(actor), nameof(bounds));
} }
public void Add(T item, Rectangle bounds) public void Add(T item, Rectangle bounds)

View File

@@ -349,7 +349,7 @@ namespace OpenRA
bool relative, WPos pos, float volumeModifier, bool attenuateVolume) bool relative, WPos pos, float volumeModifier, bool attenuateVolume)
{ {
if (ruleset == null) if (ruleset == null)
throw new ArgumentNullException("ruleset"); throw new ArgumentNullException(nameof(ruleset));
if (definition == null || DisableAllSounds || (DisableWorldSounds && soundType == SoundType.World)) if (definition == null || DisableAllSounds || (DisableWorldSounds && soundType == SoundType.World))
return false; return false;
@@ -416,7 +416,7 @@ namespace OpenRA
public bool PlayNotification(Ruleset rules, Player player, string type, string notification, string variant) public bool PlayNotification(Ruleset rules, Player player, string type, string notification, string variant)
{ {
if (rules == null) if (rules == null)
throw new ArgumentNullException("rules"); throw new ArgumentNullException(nameof(rules));
if (type == null || notification == null) if (type == null || notification == null)
return false; return false;

View File

@@ -22,7 +22,7 @@ namespace OpenRA
public static byte[] ReadBytes(this Stream s, int count) public static byte[] ReadBytes(this Stream s, int count)
{ {
if (count < 0) if (count < 0)
throw new ArgumentOutOfRangeException("count", "Non-negative number required."); throw new ArgumentOutOfRangeException(nameof(count), "Non-negative number required.");
var buffer = new byte[count]; var buffer = new byte[count];
s.ReadBytes(buffer, 0, count); s.ReadBytes(buffer, 0, count);
return buffer; return buffer;
@@ -31,7 +31,7 @@ namespace OpenRA
public static void ReadBytes(this Stream s, byte[] buffer, int offset, int count) public static void ReadBytes(this Stream s, byte[] buffer, int offset, int count)
{ {
if (count < 0) if (count < 0)
throw new ArgumentOutOfRangeException("count", "Non-negative number required."); throw new ArgumentOutOfRangeException(nameof(count), "Non-negative number required.");
while (count > 0) while (count > 0)
{ {
int bytesRead; int bytesRead;

View File

@@ -40,7 +40,7 @@ namespace OpenRA
ChannelInfo info; ChannelInfo info;
lock (Channels) lock (Channels)
if (!Channels.TryGetValue(channelName, out info)) if (!Channels.TryGetValue(channelName, out info))
throw new ArgumentException("Tried logging to non-existent channel " + channelName, "channelName"); throw new ArgumentException("Tried logging to non-existent channel " + channelName, nameof(channelName));
return info; return info;
} }

View File

@@ -51,7 +51,7 @@ namespace OpenRA.Support
public int Next(int low, int high) public int Next(int low, int high)
{ {
if (high < low) if (high < low)
throw new ArgumentOutOfRangeException("high", "Maximum value is less than the minimum value."); throw new ArgumentOutOfRangeException(nameof(high), "Maximum value is less than the minimum value.");
var diff = high - low; var diff = high - low;
if (diff <= 1) if (diff <= 1)

View File

@@ -321,7 +321,7 @@ namespace OpenRA.Traits
public void Explore(Shroud s) public void Explore(Shroud s)
{ {
if (map.Bounds != s.map.Bounds) if (map.Bounds != s.map.Bounds)
throw new ArgumentException("The map bounds of these shrouds do not match.", "s"); throw new ArgumentException("The map bounds of these shrouds do not match.", nameof(s));
foreach (var puv in map.ProjectedCells) foreach (var puv in map.ProjectedCells)
{ {

View File

@@ -124,7 +124,7 @@ namespace OpenRA
return; return;
if (!Players.Contains(localPlayer)) if (!Players.Contains(localPlayer))
throw new ArgumentException("The local player must be one of the players in the world.", "localPlayer"); throw new ArgumentException("The local player must be one of the players in the world.", nameof(localPlayer));
if (IsReplay) if (IsReplay)
return; return;

View File

@@ -158,10 +158,10 @@ namespace OpenRA.Mods.Cnc.FileSystem
static uint[] ReadBlocks(Stream s, long offset, int count) static uint[] ReadBlocks(Stream s, long offset, int count)
{ {
if (offset < 0) if (offset < 0)
throw new ArgumentOutOfRangeException("offset", "Non-negative number required."); throw new ArgumentOutOfRangeException(nameof(offset), "Non-negative number required.");
if (count < 0) if (count < 0)
throw new ArgumentOutOfRangeException("count", "Non-negative number required."); throw new ArgumentOutOfRangeException(nameof(count), "Non-negative number required.");
if (offset + (count * 2) > s.Length) if (offset + (count * 2) > s.Length)
throw new ArgumentException("Bytes to read {0} and offset {1} greater than stream length {2}.".F(count * 2, offset, s.Length)); throw new ArgumentException("Bytes to read {0} and offset {1} greater than stream length {2}.".F(count * 2, offset, s.Length));

View File

@@ -60,9 +60,9 @@ namespace OpenRA.Mods.Cnc.Graphics
public float[] TransformationMatrix(uint limb, uint frame) public float[] TransformationMatrix(uint limb, uint frame)
{ {
if (frame >= frames) if (frame >= frames)
throw new ArgumentOutOfRangeException("frame", "Only {0} frames exist.".F(frames)); throw new ArgumentOutOfRangeException(nameof(frame), "Only {0} frames exist.".F(frames));
if (limb >= limbs) if (limb >= limbs)
throw new ArgumentOutOfRangeException("limb", "Only {1} limbs exist.".F(limbs)); throw new ArgumentOutOfRangeException(nameof(limb), "Only {1} limbs exist.".F(limbs));
var l = limbData[limb]; var l = limbData[limb];
var t = new float[16]; var t = new float[16];

View File

@@ -148,7 +148,7 @@ namespace OpenRA.Mods.Common
timestamp = DateTime.UtcNow; timestamp = DateTime.UtcNow;
break; break;
default: default:
throw new ArgumentOutOfRangeException("state", state, null); throw new ArgumentOutOfRangeException(nameof(state), state, null);
} }
var richPresence = new RichPresence var richPresence = new RichPresence

View File

@@ -103,7 +103,7 @@ namespace OpenRA.Mods.Common.Traits
public void GiveExperience(int amount, bool silent = false) public void GiveExperience(int amount, bool silent = false)
{ {
if (amount < 0) if (amount < 0)
throw new ArgumentException("Revoking experience is not implemented.", "amount"); throw new ArgumentException("Revoking experience is not implemented.", nameof(amount));
if (MaxLevel == 0) if (MaxLevel == 0)
return; return;

View File

@@ -113,16 +113,16 @@ namespace OpenRA.Mods.Common.Traits
public ShroudRenderer(World world, ShroudRendererInfo info) public ShroudRenderer(World world, ShroudRendererInfo info)
{ {
if (info.ShroudVariants.Length != info.FogVariants.Length) if (info.ShroudVariants.Length != info.FogVariants.Length)
throw new ArgumentException("ShroudRenderer must define the same number of shroud and fog variants!", "info"); throw new ArgumentException("ShroudRenderer must define the same number of shroud and fog variants!", nameof(info));
if ((info.OverrideFullFog == null) ^ (info.OverrideFullShroud == null)) if ((info.OverrideFullFog == null) ^ (info.OverrideFullShroud == null))
throw new ArgumentException("ShroudRenderer cannot define overrides for only one of shroud or fog!", "info"); throw new ArgumentException("ShroudRenderer cannot define overrides for only one of shroud or fog!", nameof(info));
if (info.ShroudVariants.Length > byte.MaxValue) if (info.ShroudVariants.Length > byte.MaxValue)
throw new ArgumentException("ShroudRenderer cannot define this many shroud and fog variants.", "info"); throw new ArgumentException("ShroudRenderer cannot define this many shroud and fog variants.", nameof(info));
if (info.Index.Length >= byte.MaxValue) if (info.Index.Length >= byte.MaxValue)
throw new ArgumentException("ShroudRenderer cannot define this many indexes for shroud directions.", "info"); throw new ArgumentException("ShroudRenderer cannot define this many indexes for shroud directions.", nameof(info));
this.info = info; this.info = info;
this.world = world; this.world = world;

View File

@@ -279,7 +279,7 @@ namespace OpenRA.Mods.Common
case InaccuracyType.Absolute: case InaccuracyType.Absolute:
return inaccuracy; return inaccuracy;
default: default:
throw new InvalidEnumArgumentException("inaccuracyType", (int)inaccuracyType, typeof(InaccuracyType)); throw new InvalidEnumArgumentException(nameof(inaccuracyType), (int)inaccuracyType, typeof(InaccuracyType));
} }
} }
} }

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
string mapPath = null) string mapPath = null)
{ {
if (manifestPropertySelector == null) if (manifestPropertySelector == null)
throw new ArgumentNullException("manifestPropertySelector", "Must pass a non-null manifestPropertySelector"); throw new ArgumentNullException(nameof(manifestPropertySelector), "Must pass a non-null manifestPropertySelector");
Map map = null; Map map = null;
if (mapPath != null) if (mapPath != null)

View File

@@ -276,7 +276,7 @@ namespace OpenRA.Mods.D2k.UtilityCommands
stream = File.OpenRead(filename); stream = File.OpenRead(filename);
if (stream.Length == 0 || stream.Length % 4 != 0) if (stream.Length == 0 || stream.Length % 4 != 0)
throw new ArgumentException("The map is in an unrecognized format!", "filename"); throw new ArgumentException("The map is in an unrecognized format!", nameof(filename));
Initialize(filename); Initialize(filename);
FillMap(); FillMap();