From cfde11556fb66ef0cd528cf1a0ee062735ac12f6 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Mon, 25 Nov 2024 20:15:58 +0000 Subject: [PATCH] Enable Net 7 and Net 8 style rules. --- .editorconfig | 58 +++++++++---------- OpenRA.Game/CryptoUtil.cs | 12 ++-- OpenRA.Game/FileFormats/ReplayMetadata.cs | 3 +- OpenRA.Game/FluentBundle.cs | 3 +- OpenRA.Game/GameInformation.cs | 6 +- OpenRA.Game/Graphics/Model.cs | 6 +- OpenRA.Game/Primitives/ActionQueue.cs | 3 +- OpenRA.Game/Primitives/ActorInfoDictionary.cs | 3 +- OpenRA.Game/Primitives/Cache.cs | 3 +- OpenRA.Game/Primitives/ConcurrentCache.cs | 3 +- .../Primitives/ReadOnlyAdapterStream.cs | 3 +- OpenRA.Game/Primitives/RingBuffer.cs | 3 +- OpenRA.Game/Primitives/SegmentStream.cs | 3 +- OpenRA.Game/Scripting/ScriptMemberWrapper.cs | 2 +- OpenRA.Game/Sound/Sound.cs | 6 +- OpenRA.Mods.Cnc/SpriteLoaders/ShpTDLoader.cs | 2 +- .../EditorBrushes/EditorMarkerLayerBrush.cs | 8 +-- OpenRA.Mods.Common/FileFormats/WavReader.cs | 2 +- .../FileSystem/DefaultFileSystemLoader.cs | 2 +- OpenRA.Mods.Common/ItchIntegration.cs | 4 +- .../Lint/CheckFluentReferences.cs | 2 +- OpenRA.Mods.Common/Lint/CheckSequences.cs | 2 +- OpenRA.Mods.Common/Lint/CheckTraitLocation.cs | 2 +- .../Scripting/Global/TriggerGlobal.cs | 2 +- .../Scripting/ScriptTriggers.cs | 4 +- .../ServerTraits/SkirmishLogic.cs | 2 +- .../Widgets/Logic/Editor/SaveMapLogic.cs | 5 +- .../Logic/Ingame/ObserverStatsLogic.cs | 2 +- OpenRA.Mods.Common/Widgets/TextFieldWidget.cs | 4 +- OpenRA.Test/OpenRA.Game/CoordinateTest.cs | 3 +- .../OpenRA.Game/VariableExpressionTest.cs | 5 +- 31 files changed, 74 insertions(+), 94 deletions(-) diff --git a/.editorconfig b/.editorconfig index 8388ebc632..be5ea0069b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -748,7 +748,7 @@ dotnet_diagnostic.CA1305.severity = warning dotnet_diagnostic.CA1310.severity = warning # Specify a culture or use an invariant version. -dotnet_diagnostic.CA1311.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1311.severity = warning ### Portability and Interoperability Rules ### https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/interoperability-warnings @@ -763,19 +763,19 @@ dotnet_diagnostic.CA1417.severity = warning dotnet_diagnostic.CA1507.severity = warning # Use ArgumentNullException throw helper. -dotnet_diagnostic.CA1510.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1510.severity = warning # Use ArgumentException throw helper. -dotnet_diagnostic.CA1511.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1511.severity = warning # Use ArgumentOutOfRangeException throw helper. -dotnet_diagnostic.CA1512.severity = suggestion # TODO: Change to warning once using .NET 8 or later. +dotnet_diagnostic.CA1512.severity = warning # Use ObjectDisposedException throw helper. -dotnet_diagnostic.CA1513.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1513.severity = warning # Avoid redundant length argument. -dotnet_diagnostic.CA1514.severity = suggestion # TODO: Change to warning once using .NET 8 or later. +dotnet_diagnostic.CA1514.severity = warning ### Naming Rules ### https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/naming-warnings @@ -868,8 +868,8 @@ dotnet_diagnostic.CA1843.severity = warning # Provide memory-based overrides of async methods when subclassing 'Stream'. dotnet_diagnostic.CA1844.severity = warning -# Use span-based 'string.Concat'. (Not available on mono) -dotnet_diagnostic.CA1845.severity = none +# Use span-based 'string.Concat'. +dotnet_diagnostic.CA1845.severity = warning # Prefer AsSpan over Substring. dotnet_diagnostic.CA1846.severity = warning @@ -878,34 +878,34 @@ dotnet_diagnostic.CA1846.severity = warning dotnet_diagnostic.CA1847.severity = warning # Call async methods when in an async method. -dotnet_diagnostic.CA1849.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1849.severity = warning -# Prefer static HashData method over ComputeHash. (Not available on mono) -dotnet_diagnostic.CA1850.severity = none # TODO: Change to warning once using .NET 7 or later AND once supported by mono. +# Prefer static HashData method over ComputeHash. +dotnet_diagnostic.CA1850.severity = warning # Possible multiple enumerations of IEnumerable collection. #dotnet_code_quality.CA1851.enumeration_methods = dotnet_code_quality.CA1851.linq_chain_methods = M:OpenRA.Traits.IRenderModifier.Modify* dotnet_code_quality.CA1851.assume_method_enumerates_parameters = true -dotnet_diagnostic.CA1851.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1851.severity = warning # Seal internal types. -dotnet_diagnostic.CA1852.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1852.severity = warning # Unnecessary call to 'Dictionary.ContainsKey(key)'. -dotnet_diagnostic.CA1853.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1853.severity = warning # Prefer the IDictionary.TryGetValue(TKey, out TValue) method. -dotnet_diagnostic.CA1854.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1854.severity = warning # Use Span.Clear() instead of Span.Fill(). -dotnet_diagnostic.CA1855.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1855.severity = warning # Incorrect usage of ConstantExpected attribute. -dotnet_diagnostic.CA1856.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1856.severity = warning # The parameter expects a constant for optimal performance. -dotnet_diagnostic.CA1857.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1857.severity = warning # Use StartsWith instead of IndexOf. dotnet_diagnostic.CA1858.severity = warning @@ -917,24 +917,24 @@ dotnet_diagnostic.CA1860.severity = warning dotnet_diagnostic.CA1862.severity = warning # Use 'CompositeFormat'. -dotnet_diagnostic.CA1863.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA1863.severity = warning # Prefer the 'IDictionary.TryAdd(TKey, TValue)' method. -dotnet_diagnostic.CA1864.severity = suggestion # TODO: Change to warning once using .NET 8 or later. +dotnet_diagnostic.CA1864.severity = warning # Use 'string.Method(char)' instead of 'string.Method(string)' for string with single char. -dotnet_diagnostic.CA1865.severity = suggestion # TODO: Change to warning once using .NET 8 or later. -dotnet_diagnostic.CA1866.severity = suggestion # TODO: Change to warning once using .NET 8 or later. -dotnet_diagnostic.CA1867.severity = suggestion # TODO: Change to warning once using .NET 8 or later. +dotnet_diagnostic.CA1865.severity = warning +dotnet_diagnostic.CA1866.severity = warning +dotnet_diagnostic.CA1867.severity = warning # Unnecessary call to 'Contains' for sets. -dotnet_diagnostic.CA1868.severity = suggestion # TODO: Change to warning once using .NET 8 or later. +dotnet_diagnostic.CA1868.severity = warning # Cache and reuse 'JsonSerializerOptions' instances. -dotnet_diagnostic.CA1869.severity = suggestion # TODO: Change to warning once using .NET 8 or later. +dotnet_diagnostic.CA1869.severity = warning # Use a cached 'SearchValues' instance. -dotnet_diagnostic.CA1870.severity = suggestion # TODO: Change to warning once using .NET 8 or later. +dotnet_diagnostic.CA1870.severity = warning # Do not pass a nullable struct to 'ArgumentNullException.ThrowIfNull'. dotnet_diagnostic.CA1871.severity = warning @@ -964,7 +964,7 @@ dotnet_diagnostic.CA2016.severity = warning dotnet_diagnostic.CA2018.severity = warning # ThreadStatic fields should not use inline initialization. -dotnet_diagnostic.CA2019.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA2019.severity = warning # Don't call Enumerable.Cast or Enumerable.OfType with incompatible types. dotnet_diagnostic.CA2021.severity = warning @@ -1041,10 +1041,10 @@ dotnet_diagnostic.CA2250.severity = warning dotnet_diagnostic.CA2251.severity = warning # Ensure ThreadStatic is only used with static fields. -dotnet_diagnostic.CA2259.severity = suggestion # TODO: Change to warning once using .NET 7 or later. +dotnet_diagnostic.CA2259.severity = warning # Prefer generic overload when type is known. -dotnet_diagnostic.CA2263.severity = none # TODO: Change to warning once mono is dropped. +dotnet_diagnostic.CA2263.severity = warning # Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull'. dotnet_diagnostic.CA2264.severity = warning diff --git a/OpenRA.Game/CryptoUtil.cs b/OpenRA.Game/CryptoUtil.cs index bdfc8c558a..89285ff0fb 100644 --- a/OpenRA.Game/CryptoUtil.cs +++ b/OpenRA.Game/CryptoUtil.cs @@ -210,8 +210,7 @@ namespace OpenRA using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(parameters); - using (var csp = SHA1.Create()) - return Convert.ToBase64String(rsa.SignHash(csp.ComputeHash(data), CryptoConfig.MapNameToOID("SHA1"))); + return Convert.ToBase64String(rsa.SignHash(SHA1.HashData(data), CryptoConfig.MapNameToOID("SHA1"))); } } catch (Exception e) @@ -236,8 +235,7 @@ namespace OpenRA using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(parameters); - using (var csp = SHA1.Create()) - return rsa.VerifyHash(csp.ComputeHash(data), CryptoConfig.MapNameToOID("SHA1"), Convert.FromBase64String(signature)); + return rsa.VerifyHash(SHA1.HashData(data), CryptoConfig.MapNameToOID("SHA1"), Convert.FromBase64String(signature)); } } catch (Exception e) @@ -252,14 +250,12 @@ namespace OpenRA public static string SHA1Hash(Stream data) { - using var csp = SHA1.Create(); - return ToHex(csp.ComputeHash(data), true); + return ToHex(SHA1.HashData(data), true); } public static string SHA1Hash(byte[] data) { - using var csp = SHA1.Create(); - return ToHex(csp.ComputeHash(data), true); + return ToHex(SHA1.HashData(data), true); } public static string SHA1Hash(string data) diff --git a/OpenRA.Game/FileFormats/ReplayMetadata.cs b/OpenRA.Game/FileFormats/ReplayMetadata.cs index bfb6d009f7..57d07c15fe 100644 --- a/OpenRA.Game/FileFormats/ReplayMetadata.cs +++ b/OpenRA.Game/FileFormats/ReplayMetadata.cs @@ -27,8 +27,7 @@ namespace OpenRA.FileFormats public ReplayMetadata(GameInformation info) { - if (info == null) - throw new ArgumentNullException(nameof(info)); + ArgumentNullException.ThrowIfNull(info); GameInfo = info; } diff --git a/OpenRA.Game/FluentBundle.cs b/OpenRA.Game/FluentBundle.cs index 543c8311e5..fc7cde5262 100644 --- a/OpenRA.Game/FluentBundle.cs +++ b/OpenRA.Game/FluentBundle.cs @@ -111,8 +111,7 @@ namespace OpenRA public bool TryGetMessage(string key, out string value, object[] args = null) { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); try { diff --git a/OpenRA.Game/GameInformation.cs b/OpenRA.Game/GameInformation.cs index 71a9d3e9ca..9149b35d65 100644 --- a/OpenRA.Game/GameInformation.cs +++ b/OpenRA.Game/GameInformation.cs @@ -100,11 +100,9 @@ namespace OpenRA /// Adds the player information at start-up. public void AddPlayer(OpenRA.Player runtimePlayer, Session lobbyInfo) { - if (runtimePlayer == null) - throw new ArgumentNullException(nameof(runtimePlayer)); + ArgumentNullException.ThrowIfNull(runtimePlayer); - if (lobbyInfo == null) - throw new ArgumentNullException(nameof(lobbyInfo)); + ArgumentNullException.ThrowIfNull(lobbyInfo); // We don't care about spectators and map players if (runtimePlayer.NonCombatant || !runtimePlayer.Playable) diff --git a/OpenRA.Game/Graphics/Model.cs b/OpenRA.Game/Graphics/Model.cs index ffa3fd184d..b49e0e5d91 100644 --- a/OpenRA.Game/Graphics/Model.cs +++ b/OpenRA.Game/Graphics/Model.cs @@ -31,9 +31,9 @@ namespace OpenRA.Graphics public interface IModelWidget { - public string Palette { get; } - public float Scale { get; } - public void Setup(Func isVisible, Func getPalette, Func getPlayerPalette, + string Palette { get; } + float Scale { get; } + void Setup(Func isVisible, Func getPalette, Func getPlayerPalette, Func getScale, Func getVoxel, Func getRotation); } diff --git a/OpenRA.Game/Primitives/ActionQueue.cs b/OpenRA.Game/Primitives/ActionQueue.cs index 401be66410..f229ddab6a 100644 --- a/OpenRA.Game/Primitives/ActionQueue.cs +++ b/OpenRA.Game/Primitives/ActionQueue.cs @@ -23,8 +23,7 @@ namespace OpenRA.Primitives public void Add(Action a, long desiredTime) { - if (a == null) - throw new ArgumentNullException(nameof(a)); + ArgumentNullException.ThrowIfNull(a); lock (actions) { diff --git a/OpenRA.Game/Primitives/ActorInfoDictionary.cs b/OpenRA.Game/Primitives/ActorInfoDictionary.cs index e03ec12f1d..a11d1c2584 100644 --- a/OpenRA.Game/Primitives/ActorInfoDictionary.cs +++ b/OpenRA.Game/Primitives/ActorInfoDictionary.cs @@ -21,8 +21,7 @@ namespace OpenRA public ActorInfoDictionary(IReadOnlyDictionary dict) { - if (dict == null) - throw new ArgumentNullException(nameof(dict)); + ArgumentNullException.ThrowIfNull(dict); this.dict = new Dictionary(dict); } diff --git a/OpenRA.Game/Primitives/Cache.cs b/OpenRA.Game/Primitives/Cache.cs index 80479dc0ff..c1a60f7273 100644 --- a/OpenRA.Game/Primitives/Cache.cs +++ b/OpenRA.Game/Primitives/Cache.cs @@ -21,8 +21,7 @@ namespace OpenRA.Primitives public Cache(Func loader, IEqualityComparer c) { - if (loader == null) - throw new ArgumentNullException(nameof(loader)); + ArgumentNullException.ThrowIfNull(loader); this.loader = loader; cache = new Dictionary(c); diff --git a/OpenRA.Game/Primitives/ConcurrentCache.cs b/OpenRA.Game/Primitives/ConcurrentCache.cs index 4ffe089d2d..cc54455803 100644 --- a/OpenRA.Game/Primitives/ConcurrentCache.cs +++ b/OpenRA.Game/Primitives/ConcurrentCache.cs @@ -22,8 +22,7 @@ namespace OpenRA.Primitives public ConcurrentCache(Func loader, IEqualityComparer c) { - if (loader == null) - throw new ArgumentNullException(nameof(loader)); + ArgumentNullException.ThrowIfNull(loader); this.loader = loader; cache = new ConcurrentDictionary(c); diff --git a/OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs b/OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs index 9931df2217..295c7a8aea 100644 --- a/OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs +++ b/OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs @@ -27,8 +27,7 @@ namespace OpenRA.Primitives protected ReadOnlyAdapterStream(Stream stream) { - if (stream == null) - throw new ArgumentNullException(nameof(stream)); + ArgumentNullException.ThrowIfNull(stream); if (!stream.CanRead) throw new ArgumentException("stream must be readable.", nameof(stream)); diff --git a/OpenRA.Game/Primitives/RingBuffer.cs b/OpenRA.Game/Primitives/RingBuffer.cs index 5b05e524b8..2ad1130493 100644 --- a/OpenRA.Game/Primitives/RingBuffer.cs +++ b/OpenRA.Game/Primitives/RingBuffer.cs @@ -65,8 +65,7 @@ namespace OpenRA.Primitives public void CopyTo(T[] array, int arrayIndex) { - if (array == null) - throw new ArgumentNullException(nameof(array)); + ArgumentNullException.ThrowIfNull(array); if (arrayIndex < 0) throw new ArgumentNullException(nameof(arrayIndex)); diff --git a/OpenRA.Game/Primitives/SegmentStream.cs b/OpenRA.Game/Primitives/SegmentStream.cs index 415ba70631..9d3c235b11 100644 --- a/OpenRA.Game/Primitives/SegmentStream.cs +++ b/OpenRA.Game/Primitives/SegmentStream.cs @@ -32,8 +32,7 @@ namespace OpenRA.Primitives /// The length of the segment. public SegmentStream(Stream stream, long offset, long count) { - if (stream == null) - throw new ArgumentNullException(nameof(stream)); + ArgumentNullException.ThrowIfNull(stream); if (!stream.CanSeek) throw new ArgumentException("stream must be seekable.", nameof(stream)); if (offset < 0) diff --git a/OpenRA.Game/Scripting/ScriptMemberWrapper.cs b/OpenRA.Game/Scripting/ScriptMemberWrapper.cs index 1c4a86ef5d..5ad44d8137 100644 --- a/OpenRA.Game/Scripting/ScriptMemberWrapper.cs +++ b/OpenRA.Game/Scripting/ScriptMemberWrapper.cs @@ -150,7 +150,7 @@ namespace OpenRA.Scripting // Remove the namespace and the trailing "Info" return types.SelectMany(i => i.GetGenericArguments()) .Select(g => g.Name.Split('.', StringSplitOptions.RemoveEmptyEntries).LastOrDefault()) - .Select(s => s.EndsWith("Info", StringComparison.Ordinal) ? s.Remove(s.Length - 4, 4) : s) + .Select(s => s.EndsWith("Info", StringComparison.Ordinal) ? s[..^4] : s) .ToArray(); } } diff --git a/OpenRA.Game/Sound/Sound.cs b/OpenRA.Game/Sound/Sound.cs index bd160ba868..fa381ce87b 100644 --- a/OpenRA.Game/Sound/Sound.cs +++ b/OpenRA.Game/Sound/Sound.cs @@ -359,8 +359,7 @@ namespace OpenRA public bool PlayPredefined(SoundType soundType, Ruleset ruleset, Player player, Actor voicedActor, string type, string definition, string variant, bool relative, WPos pos, float volumeModifier, bool attenuateVolume) { - if (ruleset == null) - throw new ArgumentNullException(nameof(ruleset)); + ArgumentNullException.ThrowIfNull(ruleset); if (definition == null || DisableAllSounds || (DisableWorldSounds && soundType == SoundType.World)) return false; @@ -460,8 +459,7 @@ namespace OpenRA public bool PlayNotification(Ruleset rules, Player player, string type, string notification, string variant) { - if (rules == null) - throw new ArgumentNullException(nameof(rules)); + ArgumentNullException.ThrowIfNull(rules); if (type == null || notification == null) return false; diff --git a/OpenRA.Mods.Cnc/SpriteLoaders/ShpTDLoader.cs b/OpenRA.Mods.Cnc/SpriteLoaders/ShpTDLoader.cs index 6f5ea1be2b..2658bca903 100644 --- a/OpenRA.Mods.Cnc/SpriteLoaders/ShpTDLoader.cs +++ b/OpenRA.Mods.Cnc/SpriteLoaders/ShpTDLoader.cs @@ -75,7 +75,7 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders { enum Format : ushort { XORPrev = 0x20, XORLCW = 0x40, LCW = 0x80 } - class TrimmedFrame : ISpriteFrame + sealed class TrimmedFrame : ISpriteFrame { public SpriteFrameType Type => SpriteFrameType.Indexed8; public Size Size { get; } diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorMarkerLayerBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorMarkerLayerBrush.cs index d3e9177027..fb3b1de302 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorMarkerLayerBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorMarkerLayerBrush.cs @@ -96,7 +96,7 @@ namespace OpenRA.Mods.Common.Widgets } } - class PaintMarkerTileEditorAction : IEditorAction + sealed class PaintMarkerTileEditorAction : IEditorAction { [FluentReference("amount", "type")] const string AddedMarkerTiles = "notification-added-marker-tiles"; @@ -156,7 +156,7 @@ namespace OpenRA.Mods.Common.Widgets } } - class ClearSelectedMarkerTilesEditorAction : IEditorAction + sealed class ClearSelectedMarkerTilesEditorAction : IEditorAction { [FluentReference("amount", "type")] const string ClearedSelectedMarkerTiles = "notification-cleared-selected-marker-tiles"; @@ -195,7 +195,7 @@ namespace OpenRA.Mods.Common.Widgets } } - class ClearAllMarkerTilesEditorAction : IEditorAction + sealed class ClearAllMarkerTilesEditorAction : IEditorAction { [FluentReference("amount")] const string ClearedAllMarkerTiles = "notification-cleared-all-marker-tiles"; @@ -211,7 +211,7 @@ namespace OpenRA.Mods.Common.Widgets this.markerLayerOverlay = markerLayerOverlay; tiles = new Dictionary>(markerLayerOverlay.Tiles); - var allTilesCount = tiles.Values.Select(x => x.Count).Sum(); + var allTilesCount = tiles.Values.Sum(x => x.Count); Text = FluentProvider.GetMessage(ClearedAllMarkerTiles, "amount", allTilesCount); } diff --git a/OpenRA.Mods.Common/FileFormats/WavReader.cs b/OpenRA.Mods.Common/FileFormats/WavReader.cs index fc3e3c1bdf..d24d829b23 100644 --- a/OpenRA.Mods.Common/FileFormats/WavReader.cs +++ b/OpenRA.Mods.Common/FileFormats/WavReader.cs @@ -58,7 +58,7 @@ namespace OpenRA.Mods.Common.FileFormats var audioFormat = s.ReadInt16(); audioType = (WaveType)audioFormat; - if (!Enum.IsDefined(typeof(WaveType), audioType)) + if (!Enum.IsDefined(audioType)) throw new NotSupportedException($"Compression type {audioFormat} is not supported."); channels = s.ReadInt16(); diff --git a/OpenRA.Mods.Common/FileSystem/DefaultFileSystemLoader.cs b/OpenRA.Mods.Common/FileSystem/DefaultFileSystemLoader.cs index e4c1ed27db..15b9e3da36 100644 --- a/OpenRA.Mods.Common/FileSystem/DefaultFileSystemLoader.cs +++ b/OpenRA.Mods.Common/FileSystem/DefaultFileSystemLoader.cs @@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.FileSystem [RequireExplicitImplementation] public interface IFileSystemExternalContent { - public bool InstallContentIfRequired(ModData modData); + bool InstallContentIfRequired(ModData modData); } public class DefaultFileSystemLoader : IFileSystemLoader diff --git a/OpenRA.Mods.Common/ItchIntegration.cs b/OpenRA.Mods.Common/ItchIntegration.cs index c6ffbea379..804fe79ac1 100644 --- a/OpenRA.Mods.Common/ItchIntegration.cs +++ b/OpenRA.Mods.Common/ItchIntegration.cs @@ -19,7 +19,7 @@ namespace OpenRA.Mods.Common { public class ItchIntegration : IGlobalModData { - class User + sealed class User { [JsonProperty("url")] public string Url { get; set; } @@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common public string DisplayName { get; set; } } - class Root + sealed class Root { public User User { get; set; } } diff --git a/OpenRA.Mods.Common/Lint/CheckFluentReferences.cs b/OpenRA.Mods.Common/Lint/CheckFluentReferences.cs index 6f52d4984f..0808baa3ee 100644 --- a/OpenRA.Mods.Common/Lint/CheckFluentReferences.cs +++ b/OpenRA.Mods.Common/Lint/CheckFluentReferences.cs @@ -502,7 +502,7 @@ namespace OpenRA.Mods.Common.Lint } } - class Keys + sealed class Keys { readonly HashSet keys = new(); readonly List<(string Key, string Context)> keysWithContext = new(); diff --git a/OpenRA.Mods.Common/Lint/CheckSequences.cs b/OpenRA.Mods.Common/Lint/CheckSequences.cs index 8f8067c874..a9325eb970 100644 --- a/OpenRA.Mods.Common/Lint/CheckSequences.cs +++ b/OpenRA.Mods.Common/Lint/CheckSequences.cs @@ -59,7 +59,7 @@ namespace OpenRA.Mods.Common.Lint { // Remove the "Info" suffix. var traitName = traitInfo.GetType().Name; - traitName = traitName.Remove(traitName.Length - 4); + traitName = traitName[..^4]; var fields = Utility.GetFields(traitInfo.GetType()); foreach (var field in fields) diff --git a/OpenRA.Mods.Common/Lint/CheckTraitLocation.cs b/OpenRA.Mods.Common/Lint/CheckTraitLocation.cs index deb3e7487e..25c9a25923 100644 --- a/OpenRA.Mods.Common/Lint/CheckTraitLocation.cs +++ b/OpenRA.Mods.Common/Lint/CheckTraitLocation.cs @@ -31,7 +31,7 @@ namespace OpenRA.Mods.Common.Lint { // Remove the "Info" suffix. var traitName = traitInfo.GetType().Name; - traitName = traitName.Remove(traitName.Length - 4); + traitName = traitName[..^4]; var locations = traitLocation.SystemActors.ToString().Replace(", ", " or "); emitError($"`{traitName}` does not belong on `{actorInfo.Key}`. It is a system trait meant for {locations}."); } diff --git a/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs index 7647f436bc..2b7952647d 100644 --- a/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs @@ -567,7 +567,7 @@ namespace OpenRA.Mods.Common.Scripting "so you must not add new triggers at the same time that you are calling this function.")] public void Clear(Actor actor, string triggerName) { - var trigger = (Trigger)Enum.Parse(typeof(Trigger), triggerName); + var trigger = Enum.Parse(triggerName); if (actor == null) throw new NullReferenceException(nameof(actor)); diff --git a/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs b/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs index ce5845cd7a..b5802567e4 100644 --- a/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs +++ b/OpenRA.Mods.Common/Scripting/ScriptTriggers.cs @@ -47,7 +47,7 @@ namespace OpenRA.Mods.Common.Scripting public event Action OnProducedInternal = (a, b) => { }; public event Action OnOtherProducedInternal = (a, b) => { }; - readonly List[] triggerables = Exts.MakeArray(Enum.GetValues(typeof(Trigger)).Length, _ => new List()); + readonly List[] triggerables = Exts.MakeArray(Enum.GetValues().Length, _ => new List()); readonly struct Triggerable : IDisposable { @@ -526,7 +526,7 @@ namespace OpenRA.Mods.Common.Scripting public void ClearAll() { - foreach (Trigger t in Enum.GetValues(typeof(Trigger))) + foreach (var t in Enum.GetValues()) Clear(t); } diff --git a/OpenRA.Mods.Common/ServerTraits/SkirmishLogic.cs b/OpenRA.Mods.Common/ServerTraits/SkirmishLogic.cs index 537ef429bd..2b5fbd9547 100644 --- a/OpenRA.Mods.Common/ServerTraits/SkirmishLogic.cs +++ b/OpenRA.Mods.Common/ServerTraits/SkirmishLogic.cs @@ -22,7 +22,7 @@ namespace OpenRA.Mods.Common.Server { public class SkirmishLogic : ServerTrait, IClientJoined, INotifySyncLobbyInfo { - class SkirmishSlot + sealed class SkirmishSlot { [FieldLoader.Serialize(FromYamlKey = true)] public readonly string Slot; diff --git a/OpenRA.Mods.Common/Widgets/Logic/Editor/SaveMapLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Editor/SaveMapLogic.cs index 110d7f03c3..af30a0dd49 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Editor/SaveMapLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Editor/SaveMapLogic.cs @@ -91,7 +91,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic var visOptionTemplate = visibilityPanel.Get("VISIBILITY_TEMPLATE"); visibilityPanel.RemoveChildren(); - foreach (MapVisibility visibilityOption in Enum.GetValues(typeof(MapVisibility))) + foreach (var visibilityOption in Enum.GetValues()) { // To prevent users from breaking the game only show the 'Shellmap' option when it is already set. if (visibilityOption == MapVisibility.Shellmap && !map.Visibility.HasFlag(visibilityOption)) @@ -311,8 +311,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic try { - if (package == null) - throw new ArgumentNullException(nameof(package)); + ArgumentNullException.ThrowIfNull(package); map.Save(package); diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs index 0a28dae96b..5381e5d919 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs @@ -102,7 +102,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic this.worldRenderer = worldRenderer; MiniYaml yaml; - var keyNames = Enum.GetNames(typeof(ObserverStatsPanel)); + var keyNames = Enum.GetNames(); var statsHotkeys = new HotkeyReference[keyNames.Length]; for (var i = 0; i < keyNames.Length; i++) statsHotkeys[i] = logicArgs.TryGetValue("Statistics" + keyNames[i] + "Key", out yaml) ? modData.Hotkeys[yaml.Value] : new HotkeyReference(); diff --git a/OpenRA.Mods.Common/Widgets/TextFieldWidget.cs b/OpenRA.Mods.Common/Widgets/TextFieldWidget.cs index ab5c0f38ae..ca0a798fb2 100644 --- a/OpenRA.Mods.Common/Widgets/TextFieldWidget.cs +++ b/OpenRA.Mods.Common/Widgets/TextFieldWidget.cs @@ -321,7 +321,7 @@ namespace OpenRA.Mods.Common.Widgets if (e.Modifiers.HasModifier(Modifiers.Ctrl) && CursorPosition < Text.Length) { // Write directly to the Text backing field to avoid unnecessary validation - text = text.Remove(CursorPosition); + text = text[..CursorPosition]; CursorPosition = CursorPosition.Clamp(0, text.Length); OnTextEdited(); @@ -379,7 +379,7 @@ namespace OpenRA.Mods.Common.Widgets if ((!isOSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) || (isOSX && e.Modifiers.HasModifier(Modifiers.Alt))) text = text[..CursorPosition] + text[GetNextWhitespaceIndex()..]; else if (isOSX && e.Modifiers.HasModifier(Modifiers.Meta)) - text = text.Remove(CursorPosition); + text = text[..CursorPosition]; else text = text.Remove(CursorPosition, 1); diff --git a/OpenRA.Test/OpenRA.Game/CoordinateTest.cs b/OpenRA.Test/OpenRA.Game/CoordinateTest.cs index 71213420db..3856c1f139 100644 --- a/OpenRA.Test/OpenRA.Game/CoordinateTest.cs +++ b/OpenRA.Test/OpenRA.Game/CoordinateTest.cs @@ -10,7 +10,6 @@ #endregion using System; -using System.Linq; using NUnit.Framework; namespace OpenRA.Test @@ -21,7 +20,7 @@ namespace OpenRA.Test [TestCase(TestName = "Test CPos and MPos conversion and back again.")] public void CoarseToMapProjection() { - foreach (var gridType in Enum.GetValues(typeof(MapGridType)).Cast()) + foreach (var gridType in Enum.GetValues()) { for (var x = 0; x < 12; x++) { diff --git a/OpenRA.Test/OpenRA.Game/VariableExpressionTest.cs b/OpenRA.Test/OpenRA.Game/VariableExpressionTest.cs index 54f2ff1fcf..a53f933e36 100644 --- a/OpenRA.Test/OpenRA.Game/VariableExpressionTest.cs +++ b/OpenRA.Test/OpenRA.Game/VariableExpressionTest.cs @@ -44,9 +44,8 @@ namespace OpenRA.Test void AssertParseFailure(string expression, string errorMessage) { - var actualErrorMessage = Assert.Throws(typeof(InvalidDataException), - () => new IntegerExpression(expression).Evaluate(testValues), - expression).Message; + var actualErrorMessage = Assert.Throws( + () => new IntegerExpression(expression).Evaluate(testValues), expression).Message; Assert.That(errorMessage, Is.EqualTo(actualErrorMessage), expression + " ===> " + actualErrorMessage); }