From 52438f45e11c537b2742b2193c59811fe74e8f35 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 13 May 2024 14:50:55 +0000 Subject: [PATCH] dev: Added argumentoutofrange static methods, no functional change --- .../Db/Extensions/WarningExtensions.cs | 3 +- .../PlayingRotate/PlayingRotateService.cs | 3 +- .../Administration/Prune/PruneService.cs | 3 +- .../Role/ReactionRolesService.cs | 6 +-- .../UserPunish/UserPunishService.cs | 3 +- .../Gambling/AnimalRacing/AnimalRace.cs | 3 +- .../Modules/Gambling/Bank/BankService.cs | 18 +++---- .../Modules/Gambling/BlackJack/Player.cs | 3 +- .../Modules/Gambling/Shop/ShopService.cs | 22 ++++----- .../Gambling/Waifus/db/WaifuExtensions.cs | 4 +- .../Gambling/_common/IGamblingService.cs | 2 +- .../Gambling/_common/NewGamblingService.cs | 48 ++++++++----------- .../Modules/Music/_common/Impl/MusicQueue.cs | 9 ++-- .../_common/db/MusicPlaylistExtensions.cs | 3 +- .../Blacklist/BlacklistCommands.cs | 3 +- .../CommandCooldown/CmdCdService.cs | 5 +- .../PlainGoogleScrapeSearchResult.cs | 6 +-- .../Utility/Repeater/RepeaterService.cs | 3 +- src/NadekoBot/Modules/Xp/Club/ClubService.cs | 3 +- .../Services/Impl/GoogleApiService.cs | 17 +++---- .../Collections/ConcurrentHashSet.cs | 8 +--- .../_common/Abstractions/NadekoRandom.cs | 4 +- .../_common/Abstractions/QueueRunner.cs | 3 +- .../_common/Attributes/RatelimitAttribute.cs | 3 +- 24 files changed, 69 insertions(+), 116 deletions(-) diff --git a/src/NadekoBot/Db/Extensions/WarningExtensions.cs b/src/NadekoBot/Db/Extensions/WarningExtensions.cs index 20d8c1338..c254f923e 100644 --- a/src/NadekoBot/Db/Extensions/WarningExtensions.cs +++ b/src/NadekoBot/Db/Extensions/WarningExtensions.cs @@ -22,8 +22,7 @@ public static class WarningExtensions string mod, int index) { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); + ArgumentOutOfRangeException.ThrowIfNegative(index); var warn = warnings.AsQueryable() .Where(x => x.GuildId == guildId && x.UserId == userId) diff --git a/src/NadekoBot/Modules/Administration/PlayingRotate/PlayingRotateService.cs b/src/NadekoBot/Modules/Administration/PlayingRotate/PlayingRotateService.cs index 0b48173ba..851830acf 100644 --- a/src/NadekoBot/Modules/Administration/PlayingRotate/PlayingRotateService.cs +++ b/src/NadekoBot/Modules/Administration/PlayingRotate/PlayingRotateService.cs @@ -69,8 +69,7 @@ public sealed class PlayingRotateService : INService, IReadyExecutor public async Task RemovePlayingAsync(int index) { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); + ArgumentOutOfRangeException.ThrowIfNegative(index); await using var uow = _db.GetDbContext(); var toRemove = await uow.Set().AsQueryable().AsNoTracking().Skip(index).FirstOrDefaultAsync(); diff --git a/src/NadekoBot/Modules/Administration/Prune/PruneService.cs b/src/NadekoBot/Modules/Administration/Prune/PruneService.cs index 9feeee53e..6824831a4 100644 --- a/src/NadekoBot/Modules/Administration/Prune/PruneService.cs +++ b/src/NadekoBot/Modules/Administration/Prune/PruneService.cs @@ -22,8 +22,7 @@ public class PruneService : INService ArgumentNullException.ThrowIfNull(channel, nameof(channel)); var originalAmount = amount; - if (amount <= 0) - throw new ArgumentOutOfRangeException(nameof(amount)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount); using var cancelSource = new CancellationTokenSource(); if (!_pruningGuilds.TryAdd(channel.GuildId, cancelSource)) diff --git a/src/NadekoBot/Modules/Administration/Role/ReactionRolesService.cs b/src/NadekoBot/Modules/Administration/Role/ReactionRolesService.cs index 7a71f85ba..91b64d488 100644 --- a/src/NadekoBot/Modules/Administration/Role/ReactionRolesService.cs +++ b/src/NadekoBot/Modules/Administration/Role/ReactionRolesService.cs @@ -250,11 +250,9 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR int group = 0, int levelReq = 0) { - if (group < 0) - throw new ArgumentOutOfRangeException(nameof(group)); + ArgumentOutOfRangeException.ThrowIfNegative(group); - if (levelReq < 0) - throw new ArgumentOutOfRangeException(nameof(group)); + ArgumentOutOfRangeException.ThrowIfNegative(levelReq); await using var ctx = _db.GetDbContext(); diff --git a/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs b/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs index 05a303f3b..08cb734f2 100644 --- a/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs +++ b/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs @@ -64,8 +64,7 @@ public class UserPunishService : INService, IReadyExecutor long weight, string reason) { - if (weight <= 0) - throw new ArgumentOutOfRangeException(nameof(weight)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(weight); var modName = mod.ToString(); diff --git a/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRace.cs b/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRace.cs index b56e13109..7bb0dd88a 100644 --- a/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRace.cs +++ b/src/NadekoBot/Modules/Gambling/AnimalRacing/AnimalRace.cs @@ -61,8 +61,7 @@ public sealed class AnimalRace : IDisposable public async Task JoinRace(ulong userId, string userName, long bet = 0) { - if (bet < 0) - throw new ArgumentOutOfRangeException(nameof(bet)); + ArgumentOutOfRangeException.ThrowIfNegative(bet); var user = new AnimalRacingUser(userName, userId, bet); diff --git a/src/NadekoBot/Modules/Gambling/Bank/BankService.cs b/src/NadekoBot/Modules/Gambling/Bank/BankService.cs index 19e87812a..5e0c6e5ca 100644 --- a/src/NadekoBot/Modules/Gambling/Bank/BankService.cs +++ b/src/NadekoBot/Modules/Gambling/Bank/BankService.cs @@ -17,8 +17,7 @@ public sealed class BankService : IBankService, INService public async Task AwardAsync(ulong userId, long amount) { - if (amount <= 0) - throw new ArgumentOutOfRangeException(nameof(amount)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount); await using var ctx = _db.GetDbContext(); await ctx.GetTable() @@ -41,9 +40,8 @@ public sealed class BankService : IBankService, INService public async Task TakeAsync(ulong userId, long amount) { - if (amount <= 0) - throw new ArgumentOutOfRangeException(nameof(amount)); - + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount); + await using var ctx = _db.GetDbContext(); var rows = await ctx.Set() .ToLinqToDBTable() @@ -58,9 +56,8 @@ public sealed class BankService : IBankService, INService public async Task DepositAsync(ulong userId, long amount) { - if (amount <= 0) - throw new ArgumentOutOfRangeException(nameof(amount)); - + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount); + if (!await _cur.RemoveAsync(userId, amount, new("bank", "deposit"))) return false; @@ -86,9 +83,8 @@ public sealed class BankService : IBankService, INService public async Task WithdrawAsync(ulong userId, long amount) { - if (amount <= 0) - throw new ArgumentOutOfRangeException(nameof(amount)); - + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount); + await using var ctx = _db.GetDbContext(); var rows = await ctx.Set() .ToLinqToDBTable() diff --git a/src/NadekoBot/Modules/Gambling/BlackJack/Player.cs b/src/NadekoBot/Modules/Gambling/BlackJack/Player.cs index 6bb256c8a..b86ec610f 100644 --- a/src/NadekoBot/Modules/Gambling/BlackJack/Player.cs +++ b/src/NadekoBot/Modules/Gambling/BlackJack/Player.cs @@ -49,8 +49,7 @@ public class User : Player public User(IUser user, long bet) { - if (bet <= 0) - throw new ArgumentOutOfRangeException(nameof(bet)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bet); Bet = bet; DiscordUser = user; diff --git a/src/NadekoBot/Modules/Gambling/Shop/ShopService.cs b/src/NadekoBot/Modules/Gambling/Shop/ShopService.cs index 264cb74e0..029c7f1fe 100644 --- a/src/NadekoBot/Modules/Gambling/Shop/ShopService.cs +++ b/src/NadekoBot/Modules/Gambling/Shop/ShopService.cs @@ -20,10 +20,8 @@ public class ShopService : IShopService, INService public async Task ChangeEntryPriceAsync(ulong guildId, int index, int newPrice) { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - if (newPrice <= 0) - throw new ArgumentOutOfRangeException(nameof(newPrice)); + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(newPrice); await using var uow = _db.GetDbContext(); var entries = GetEntriesInternal(uow, guildId); @@ -38,8 +36,8 @@ public class ShopService : IShopService, INService public async Task ChangeEntryNameAsync(ulong guildId, int index, string newName) { - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); + ArgumentOutOfRangeException.ThrowIfNegative(index); + if (string.IsNullOrWhiteSpace(newName)) throw new ArgumentNullException(nameof(newName)); @@ -56,10 +54,8 @@ public class ShopService : IShopService, INService public async Task SwapEntriesAsync(ulong guildId, int index1, int index2) { - if (index1 < 0) - throw new ArgumentOutOfRangeException(nameof(index1)); - if (index2 < 0) - throw new ArgumentOutOfRangeException(nameof(index2)); + ArgumentOutOfRangeException.ThrowIfNegative(index1); + ArgumentOutOfRangeException.ThrowIfNegative(index2); await using var uow = _db.GetDbContext(); var entries = GetEntriesInternal(uow, guildId); @@ -76,10 +72,8 @@ public class ShopService : IShopService, INService public async Task MoveEntryAsync(ulong guildId, int fromIndex, int toIndex) { - if (fromIndex < 0) - throw new ArgumentOutOfRangeException(nameof(fromIndex)); - if (toIndex < 0) - throw new ArgumentOutOfRangeException(nameof(toIndex)); + ArgumentOutOfRangeException.ThrowIfNegative(fromIndex); + ArgumentOutOfRangeException.ThrowIfNegative(toIndex); await using var uow = _db.GetDbContext(); var entries = GetEntriesInternal(uow, guildId); diff --git a/src/NadekoBot/Modules/Gambling/Waifus/db/WaifuExtensions.cs b/src/NadekoBot/Modules/Gambling/Waifus/db/WaifuExtensions.cs index 024817b99..c4892aa36 100644 --- a/src/NadekoBot/Modules/Gambling/Waifus/db/WaifuExtensions.cs +++ b/src/NadekoBot/Modules/Gambling/Waifus/db/WaifuExtensions.cs @@ -27,8 +27,8 @@ public static class WaifuExtensions public static IEnumerable GetTop(this DbSet waifus, int count, int skip = 0) { - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); + ArgumentOutOfRangeException.ThrowIfNegative(count); + if (count == 0) return new List(); diff --git a/src/NadekoBot/Modules/Gambling/_common/IGamblingService.cs b/src/NadekoBot/Modules/Gambling/_common/IGamblingService.cs index 1f9401a88..073a11a08 100644 --- a/src/NadekoBot/Modules/Gambling/_common/IGamblingService.cs +++ b/src/NadekoBot/Modules/Gambling/_common/IGamblingService.cs @@ -14,5 +14,5 @@ public interface IGamblingService Task> SlotAsync(ulong userId, long amount); Task FlipAsync(int count); Task> RpsAsync(ulong userId, long amount, byte pick); - Task> BetDrawAsync(ulong userId, long amount, byte? guessValue, byte? guessColor); + Task> BetDrawAsync(ulong userId, long amount, byte? maybeGuessValue, byte? maybeGuessColor); } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Gambling/_common/NewGamblingService.cs b/src/NadekoBot/Modules/Gambling/_common/NewGamblingService.cs index 57aa0fe2f..0b7d92476 100644 --- a/src/NadekoBot/Modules/Gambling/_common/NewGamblingService.cs +++ b/src/NadekoBot/Modules/Gambling/_common/NewGamblingService.cs @@ -20,9 +20,8 @@ public sealed class NewGamblingService : IGamblingService, INService public async Task> LulaAsync(ulong userId, long amount) { - if (amount < 0) - throw new ArgumentOutOfRangeException(nameof(amount)); - + ArgumentOutOfRangeException.ThrowIfNegative(amount); + if (amount > 0) { var isTakeSuccess = await _cs.RemoveAsync(userId, amount, new("lula", "bet")); @@ -47,8 +46,7 @@ public sealed class NewGamblingService : IGamblingService, INService public async Task> BetRollAsync(ulong userId, long amount) { - if (amount < 0) - throw new ArgumentOutOfRangeException(nameof(amount)); + ArgumentOutOfRangeException.ThrowIfNegative(amount); if (amount > 0) { @@ -77,11 +75,9 @@ public sealed class NewGamblingService : IGamblingService, INService public async Task> BetFlipAsync(ulong userId, long amount, byte guess) { - if (amount < 0) - throw new ArgumentOutOfRangeException(nameof(amount)); + ArgumentOutOfRangeException.ThrowIfNegative(amount); - if (guess > 1) - throw new ArgumentOutOfRangeException(nameof(guess)); + ArgumentOutOfRangeException.ThrowIfGreaterThan(guess, 1); if (amount > 0) { @@ -105,19 +101,18 @@ public sealed class NewGamblingService : IGamblingService, INService return result; } - public async Task> BetDrawAsync(ulong userId, long amount, byte? guessValue, byte? guessColor) + public async Task> BetDrawAsync(ulong userId, long amount, byte? maybeGuessValue, byte? maybeGuessColor) { - if (amount < 0) - throw new ArgumentOutOfRangeException(nameof(amount)); + ArgumentOutOfRangeException.ThrowIfNegative(amount); - if (guessColor is null && guessValue is null) + if (maybeGuessColor is null && maybeGuessValue is null) throw new ArgumentNullException(); - if (guessColor > 1) - throw new ArgumentOutOfRangeException(nameof(guessColor)); + if (maybeGuessColor > 1) + throw new ArgumentOutOfRangeException(nameof(maybeGuessColor)); - if (guessValue > 1) - throw new ArgumentOutOfRangeException(nameof(guessValue)); + if (maybeGuessValue > 1) + throw new ArgumentOutOfRangeException(nameof(maybeGuessValue)); if (amount > 0) { @@ -130,7 +125,7 @@ public sealed class NewGamblingService : IGamblingService, INService } var game = new BetdrawGame(); - var result = game.Draw((BetdrawValueGuess?)guessValue, (BetdrawColorGuess?)guessColor, amount); + var result = game.Draw((BetdrawValueGuess?)maybeGuessValue, (BetdrawColorGuess?)maybeGuessColor, amount); var won = (long)result.Won; if (won > 0) @@ -143,9 +138,8 @@ public sealed class NewGamblingService : IGamblingService, INService public async Task> SlotAsync(ulong userId, long amount) { - if (amount < 0) - throw new ArgumentOutOfRangeException(nameof(amount)); - + ArgumentOutOfRangeException.ThrowIfNegative(amount); + if (amount > 0) { var isTakeSuccess = await _cs.RemoveAsync(userId, amount, new("slot", "bet")); @@ -170,9 +164,8 @@ public sealed class NewGamblingService : IGamblingService, INService public Task FlipAsync(int count) { - if (count < 1) - throw new ArgumentOutOfRangeException(nameof(count)); - + ArgumentOutOfRangeException.ThrowIfLessThan(count, 1); + var game = new BetflipGame(0); var results = new FlipResult[count]; @@ -242,11 +235,8 @@ public sealed class NewGamblingService : IGamblingService, INService public async Task> RpsAsync(ulong userId, long amount, byte pick) { - if (amount < 0) - throw new ArgumentOutOfRangeException(nameof(amount)); - - if (pick > 2) - throw new ArgumentOutOfRangeException(nameof(pick)); + ArgumentOutOfRangeException.ThrowIfNegative(amount); + ArgumentOutOfRangeException.ThrowIfGreaterThan(pick, 2); if (amount > 0) { diff --git a/src/NadekoBot/Modules/Music/_common/Impl/MusicQueue.cs b/src/NadekoBot/Modules/Music/_common/Impl/MusicQueue.cs index a1c7d4531..ed7d39555 100644 --- a/src/NadekoBot/Modules/Music/_common/Impl/MusicQueue.cs +++ b/src/NadekoBot/Modules/Music/_common/Impl/MusicQueue.cs @@ -201,12 +201,9 @@ public sealed partial class MusicQueue : IMusicQueue public IQueuedTrackInfo? MoveTrack(int from, int to) { - if (from < 0) - throw new ArgumentOutOfRangeException(nameof(from)); - if (to < 0) - throw new ArgumentOutOfRangeException(nameof(to)); - if (to == from) - throw new ArgumentException($"{nameof(from)} and {nameof(to)} must be different"); + ArgumentOutOfRangeException.ThrowIfNegative(from); + ArgumentOutOfRangeException.ThrowIfNegative(to); + ArgumentOutOfRangeException.ThrowIfEqual(to, from); lock (_locker) { diff --git a/src/NadekoBot/Modules/Music/_common/db/MusicPlaylistExtensions.cs b/src/NadekoBot/Modules/Music/_common/db/MusicPlaylistExtensions.cs index 4e4c0ecbe..814b0c84a 100644 --- a/src/NadekoBot/Modules/Music/_common/db/MusicPlaylistExtensions.cs +++ b/src/NadekoBot/Modules/Music/_common/db/MusicPlaylistExtensions.cs @@ -8,8 +8,7 @@ public static class MusicPlaylistExtensions { public static List GetPlaylistsOnPage(this DbSet playlists, int num) { - if (num < 1) - throw new ArgumentOutOfRangeException(nameof(num)); + ArgumentOutOfRangeException.ThrowIfLessThan(num, 1); return playlists.AsQueryable().Skip((num - 1) * 20).Take(20).Include(pl => pl.Songs).ToList(); } diff --git a/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistCommands.cs b/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistCommands.cs index 1e62bdc6f..47855d0ce 100644 --- a/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistCommands.cs +++ b/src/NadekoBot/Modules/Permissions/Blacklist/BlacklistCommands.cs @@ -16,8 +16,7 @@ public partial class Permissions private async Task ListBlacklistInternal(string title, BlacklistType type, int page = 0) { - if (page < 0) - throw new ArgumentOutOfRangeException(nameof(page)); + ArgumentOutOfRangeException.ThrowIfNegative(page); var list = _service.GetBlacklist(); var allItems = await list.Where(x => x.Type == type) diff --git a/src/NadekoBot/Modules/Permissions/CommandCooldown/CmdCdService.cs b/src/NadekoBot/Modules/Permissions/CommandCooldown/CmdCdService.cs index 1f351f104..1474cc4a1 100644 --- a/src/NadekoBot/Modules/Permissions/CommandCooldown/CmdCdService.cs +++ b/src/NadekoBot/Modules/Permissions/CommandCooldown/CmdCdService.cs @@ -112,9 +112,8 @@ public sealed class CmdCdService : IExecPreCommand, IReadyExecutor, INService public void AddCooldown(ulong guildId, string name, int secs) { - if (secs <= 0) - throw new ArgumentOutOfRangeException(nameof(secs)); - + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(secs); + var sett = _settings.GetOrAdd(guildId, static _ => new()); sett[name] = secs; diff --git a/src/NadekoBot/Modules/Searches/Search/GoogleScrape/PlainGoogleScrapeSearchResult.cs b/src/NadekoBot/Modules/Searches/Search/GoogleScrape/PlainGoogleScrapeSearchResult.cs index d3aeb7357..b02ff9636 100644 --- a/src/NadekoBot/Modules/Searches/Search/GoogleScrape/PlainGoogleScrapeSearchResult.cs +++ b/src/NadekoBot/Modules/Searches/Search/GoogleScrape/PlainGoogleScrapeSearchResult.cs @@ -2,7 +2,7 @@ public class PlainGoogleScrapeSearchResult : ISearchResult { - public string? Answer { get; init; } = null!; - public IReadOnlyCollection Entries { get; init; } = null!; - public ISearchResultInformation Info { get; init; } = null!; + public required string? Answer { get; init; } + public required IReadOnlyCollection Entries { get; init; } + public required ISearchResultInformation Info { get; init; } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Utility/Repeater/RepeaterService.cs b/src/NadekoBot/Modules/Utility/Repeater/RepeaterService.cs index 9bf15dca3..e1edb86a0 100644 --- a/src/NadekoBot/Modules/Utility/Repeater/RepeaterService.cs +++ b/src/NadekoBot/Modules/Utility/Repeater/RepeaterService.cs @@ -363,8 +363,7 @@ public sealed class RepeaterService : IReadyExecutor, INService public async Task RemoveByIndexAsync(ulong guildId, int index) { - if (index > MAX_REPEATERS * 2) - throw new ArgumentOutOfRangeException(nameof(index)); + ArgumentOutOfRangeException.ThrowIfGreaterThan(index, MAX_REPEATERS * 2); await using var uow = _db.GetDbContext(); var toRemove = await uow.Set() diff --git a/src/NadekoBot/Modules/Xp/Club/ClubService.cs b/src/NadekoBot/Modules/Xp/Club/ClubService.cs index e31fa1bb6..17305c3a8 100644 --- a/src/NadekoBot/Modules/Xp/Club/ClubService.cs +++ b/src/NadekoBot/Modules/Xp/Club/ClubService.cs @@ -332,8 +332,7 @@ public class ClubService : INService, IClubService public List GetClubLeaderboardPage(int page) { - if (page < 0) - throw new ArgumentOutOfRangeException(nameof(page)); + ArgumentOutOfRangeException.ThrowIfNegative(page); using var uow = _db.GetDbContext(); return uow.Set().GetClubLeaderboardPage(page); diff --git a/src/NadekoBot/Services/Impl/GoogleApiService.cs b/src/NadekoBot/Services/Impl/GoogleApiService.cs index 8aa8b0dd1..d90422be1 100644 --- a/src/NadekoBot/Services/Impl/GoogleApiService.cs +++ b/src/NadekoBot/Services/Impl/GoogleApiService.cs @@ -43,8 +43,7 @@ public sealed partial class GoogleApiService : IGoogleApiService, INService if (string.IsNullOrWhiteSpace(keywords)) throw new ArgumentNullException(nameof(keywords)); - if (count <= 0) - throw new ArgumentOutOfRangeException(nameof(count)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(count); var match = _plRegex.Match(keywords); if (match.Length > 1) @@ -62,9 +61,8 @@ public sealed partial class GoogleApiService : IGoogleApiService, INService if (string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id)); - if (count <= 0) - throw new ArgumentOutOfRangeException(nameof(count)); - + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(count); + var query = _yt.Search.List("snippet"); query.MaxResults = count; query.Q = id; @@ -82,8 +80,7 @@ public sealed partial class GoogleApiService : IGoogleApiService, INService if (string.IsNullOrWhiteSpace(keywords)) throw new ArgumentNullException(nameof(keywords)); - if (count <= 0) - throw new ArgumentOutOfRangeException(nameof(count)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(count); var query = _yt.Search.List("snippet"); query.MaxResults = count; @@ -100,8 +97,7 @@ public sealed partial class GoogleApiService : IGoogleApiService, INService if (string.IsNullOrWhiteSpace(keywords)) throw new ArgumentNullException(nameof(keywords)); - if (count <= 0) - throw new ArgumentOutOfRangeException(nameof(count)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(count); var query = _yt.Search.List("snippet"); query.MaxResults = count; @@ -150,8 +146,7 @@ public sealed partial class GoogleApiService : IGoogleApiService, INService if (string.IsNullOrWhiteSpace(playlistId)) throw new ArgumentNullException(nameof(playlistId)); - if (count <= 0) - throw new ArgumentOutOfRangeException(nameof(count)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(count); string nextPageToken = null; diff --git a/src/NadekoBot/_common/Abstractions/Collections/ConcurrentHashSet.cs b/src/NadekoBot/_common/Abstractions/Collections/ConcurrentHashSet.cs index 581df4b6c..7d5bbc1c8 100644 --- a/src/NadekoBot/_common/Abstractions/Collections/ConcurrentHashSet.cs +++ b/src/NadekoBot/_common/Abstractions/Collections/ConcurrentHashSet.cs @@ -46,12 +46,8 @@ public sealed class ConcurrentHashSet : IReadOnlyCollection, ICollection= array.Length) - throw new ArgumentOutOfRangeException(nameof(arrayIndex)); + ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, array.Length); CopyToInternal(array, arrayIndex); } diff --git a/src/NadekoBot/_common/Abstractions/NadekoRandom.cs b/src/NadekoBot/_common/Abstractions/NadekoRandom.cs index fa156be00..1bc11fa05 100644 --- a/src/NadekoBot/_common/Abstractions/NadekoRandom.cs +++ b/src/NadekoBot/_common/Abstractions/NadekoRandom.cs @@ -38,8 +38,8 @@ public sealed class NadekoRandom : Random public long NextLong(long minValue, long maxValue) { - if (minValue > maxValue) - throw new ArgumentOutOfRangeException(nameof(maxValue)); + ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue); + if (minValue == maxValue) return minValue; var bytes = new byte[sizeof(long)]; diff --git a/src/NadekoBot/_common/Abstractions/QueueRunner.cs b/src/NadekoBot/_common/Abstractions/QueueRunner.cs index 14384fd92..54caaf2c1 100644 --- a/src/NadekoBot/_common/Abstractions/QueueRunner.cs +++ b/src/NadekoBot/_common/Abstractions/QueueRunner.cs @@ -9,8 +9,7 @@ public sealed class QueueRunner public QueueRunner(int delayMs = 0, int maxCapacity = -1) { - if (delayMs < 0) - throw new ArgumentOutOfRangeException(nameof(delayMs)); + ArgumentOutOfRangeException.ThrowIfNegative(delayMs); _delayMs = delayMs; _channel = maxCapacity switch diff --git a/src/NadekoBot/_common/Attributes/RatelimitAttribute.cs b/src/NadekoBot/_common/Attributes/RatelimitAttribute.cs index 92c411d04..5fd83de7e 100644 --- a/src/NadekoBot/_common/Attributes/RatelimitAttribute.cs +++ b/src/NadekoBot/_common/Attributes/RatelimitAttribute.cs @@ -9,8 +9,7 @@ public sealed class RatelimitAttribute : PreconditionAttribute public RatelimitAttribute(int seconds) { - if (seconds <= 0) - throw new ArgumentOutOfRangeException(nameof(seconds)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(seconds); Seconds = seconds; }