From 5254348819b696a7493823f3602052138bbfc9c4 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Wed, 5 Apr 2023 19:09:23 +0100 Subject: [PATCH] Fix IDE0056 --- .editorconfig | 2 +- OpenRA.Game/Graphics/RgbaColorRenderer.cs | 2 +- OpenRA.Game/MiniYaml.cs | 4 ++-- OpenRA.Game/Support/VariableExpression.cs | 6 +++--- OpenRA.Mods.Cnc/UtilityCommands/Glob.cs | 8 ++++---- OpenRA.Mods.Common/Activities/Move/Move.cs | 4 ++-- OpenRA.Mods.Common/HitShapes/Polygon.cs | 4 ++-- .../Traits/Conditions/GrantConditionOnAttack.cs | 2 +- OpenRA.Mods.Common/Traits/World/WarheadDebugOverlay.cs | 2 +- OpenRA.Mods.Common/Util.cs | 2 +- .../UtilityCommands/ConvertSpriteToPngCommand.cs | 6 +++--- OpenRA.Mods.Common/Warheads/SpreadDamageWarhead.cs | 4 ++-- .../Widgets/TextNotificationsDisplayWidget.cs | 6 +++--- OpenRA.Mods.D2k/Traits/AttractsWorms.cs | 2 +- OpenRA.Utility/Program.cs | 2 +- 15 files changed, 28 insertions(+), 28 deletions(-) diff --git a/.editorconfig b/.editorconfig index 93badf243a..9b6d6a510c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -141,7 +141,7 @@ dotnet_diagnostic.IDE0074.severity = silent # Requires C# 8 - TODO Consider enab # IDE0056 Use index operator #csharp_style_prefer_index_operator = true -dotnet_diagnostic.IDE0056.severity = silent # Requires C# 8 - TODO Consider enabling +dotnet_diagnostic.IDE0056.severity = warning # IDE0057 Use range operator #csharp_style_prefer_range_operator = true diff --git a/OpenRA.Game/Graphics/RgbaColorRenderer.cs b/OpenRA.Game/Graphics/RgbaColorRenderer.cs index 5e74088d45..0b4c63d5d0 100644 --- a/OpenRA.Game/Graphics/RgbaColorRenderer.cs +++ b/OpenRA.Game/Graphics/RgbaColorRenderer.cs @@ -138,7 +138,7 @@ namespace OpenRA.Graphics // Segment is part of closed loop if (closed) { - var prev = points[points.Length - 1]; + var prev = points[^1]; var prevDir = (start - prev) / (start - prev).XY.Length; var prevCorner = width / 2 * new float3(-prevDir.Y, prevDir.X, prevDir.Z); ca = IntersectionOf(start - prevCorner, prevDir, start - corner, dir); diff --git a/OpenRA.Game/MiniYaml.cs b/OpenRA.Game/MiniYaml.cs index 076005c9a6..77ec2ea3e2 100644 --- a/OpenRA.Game/MiniYaml.cs +++ b/OpenRA.Game/MiniYaml.cs @@ -209,7 +209,7 @@ namespace OpenRA while (levels.Count > level + 1) { - levels[levels.Count - 1].TrimExcess(); + levels[^1].TrimExcess(); levels.RemoveAt(levels.Count - 1); } @@ -260,7 +260,7 @@ namespace OpenRA { // Remove leading/trailing whitespace guards var trimLeading = value[0] == '\\' && (value[1] == ' ' || value[1] == '\t') ? 1 : 0; - var trimTrailing = value[value.Length - 1] == '\\' && (value[value.Length - 2] == ' ' || value[value.Length - 2] == '\t') ? 1 : 0; + var trimTrailing = value[^1] == '\\' && (value[^2] == ' ' || value[^2] == '\t') ? 1 : 0; if (trimLeading + trimTrailing > 0) value = value.Slice(trimLeading, value.Length - trimLeading - trimTrailing); diff --git a/OpenRA.Game/Support/VariableExpression.cs b/OpenRA.Game/Support/VariableExpression.cs index 5878465147..2906f6a420 100644 --- a/OpenRA.Game/Support/VariableExpression.cs +++ b/OpenRA.Game/Support/VariableExpression.cs @@ -719,12 +719,12 @@ namespace OpenRA.Support readonly List expressions = new List(); readonly List types = new List(); - public ExpressionType PeekType() { return types[types.Count - 1]; } + public ExpressionType PeekType() { return types[^1]; } public Expression Peek(ExpressionType toType) { - var fromType = types[types.Count - 1]; - var expression = expressions[expressions.Count - 1]; + var fromType = types[^1]; + var expression = expressions[^1]; if (toType == fromType) return expression; diff --git a/OpenRA.Mods.Cnc/UtilityCommands/Glob.cs b/OpenRA.Mods.Cnc/UtilityCommands/Glob.cs index 068b6fbcd9..b11fe898f0 100644 --- a/OpenRA.Mods.Cnc/UtilityCommands/Glob.cs +++ b/OpenRA.Mods.Cnc/UtilityCommands/Glob.cs @@ -73,13 +73,13 @@ namespace OpenRA.Mods.Cnc.UtilityCommands parts.Insert(0, "." + Path.DirectorySeparatorChar); // If the last entry ends with a directory separator, append a '*' - if (parts[parts.Count - 1][parts[parts.Count - 1].Length - 1] == Path.DirectorySeparatorChar - || parts[parts.Count - 1][parts[parts.Count - 1].Length - 1] == Path.AltDirectorySeparatorChar) + if (parts[^1][^1] == Path.DirectorySeparatorChar + || parts[^1][^1] == Path.AltDirectorySeparatorChar) parts.Add("*"); var root = parts[0]; var dirs = parts.Skip(1).Take(parts.Count - 2).ToList(); - var file = parts[parts.Count - 1]; + var file = parts[^1]; foreach (var path in Expand(root, dirs, 0, file)) yield return path; @@ -105,7 +105,7 @@ namespace OpenRA.Mods.Cnc.UtilityCommands } else { - if (dir[dir.Length - 1] == Path.DirectorySeparatorChar || dir[dir.Length - 1] == Path.AltDirectorySeparatorChar) + if (dir[^1] == Path.DirectorySeparatorChar || dir[^1] == Path.AltDirectorySeparatorChar) dir = dir.Substring(0, dir.Length - 1); foreach (var subDir in Directory.EnumerateDirectories(basePath, dir, SearchOption.TopDirectoryOnly)) foreach (var s in Expand(subDir, dirs, dirIndex + 1, file)) diff --git a/OpenRA.Mods.Common/Activities/Move/Move.cs b/OpenRA.Mods.Common/Activities/Move/Move.cs index 9ea28eb6bd..5675ce28e3 100644 --- a/OpenRA.Mods.Common/Activities/Move/Move.cs +++ b/OpenRA.Mods.Common/Activities/Move/Move.cs @@ -202,7 +202,7 @@ namespace OpenRA.Mods.Common.Activities if (path.Count == 0) return null; - var nextCell = path[path.Count - 1]; + var nextCell = path[^1]; // Something else might have moved us, so the path is no longer valid. if (!Util.AreAdjacentCells(mobile.ToCell, nextCell)) @@ -280,7 +280,7 @@ namespace OpenRA.Mods.Common.Activities if (newPath.Count != 0) { path = newPath; - var newCell = path[path.Count - 1]; + var newCell = path[^1]; path.RemoveAt(path.Count - 1); return (newCell, mobile.GetAvailableSubCell(nextCell, mobile.FromSubCell, ignoreActor)); diff --git a/OpenRA.Mods.Common/HitShapes/Polygon.cs b/OpenRA.Mods.Common/HitShapes/Polygon.cs index 6bef362ae1..451fabc9c8 100644 --- a/OpenRA.Mods.Common/HitShapes/Polygon.cs +++ b/OpenRA.Mods.Common/HitShapes/Polygon.cs @@ -53,7 +53,7 @@ namespace OpenRA.Mods.Common.HitShapes combatOverlayVertsTop = Points.Select(p => new WVec(p.X, p.Y, VerticalTopOffset)).ToArray(); combatOverlayVertsBottom = Points.Select(p => new WVec(p.X, p.Y, VerticalBottomOffset)).ToArray(); squares = new int[Points.Length]; - squares[0] = (Points[0] - Points[Points.Length - 1]).LengthSquared; + squares[0] = (Points[0] - Points[^1]).LengthSquared; for (var i = 1; i < Points.Length; i++) squares[i] = (Points[i] - Points[i - 1]).LengthSquared; } @@ -89,7 +89,7 @@ namespace OpenRA.Mods.Common.HitShapes if (Points.PolygonContains(p)) return new WDist(z); - var min2 = DistanceSquaredFromLineSegment(p, Points[Points.Length - 1], Points[0], squares[0]); + var min2 = DistanceSquaredFromLineSegment(p, Points[^1], Points[0], squares[0]); for (var i = 1; i < Points.Length; i++) { var d2 = DistanceSquaredFromLineSegment(p, Points[i - 1], Points[i], squares[i]); diff --git a/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnAttack.cs b/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnAttack.cs index 3f8ce1c36c..d1e24091db 100644 --- a/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnAttack.cs +++ b/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnAttack.cs @@ -144,7 +144,7 @@ namespace OpenRA.Mods.Common.Traits shotsFired++; var requiredShots = tokens.Count < Info.RequiredShotsPerInstance.Length ? Info.RequiredShotsPerInstance[tokens.Count] - : Info.RequiredShotsPerInstance[Info.RequiredShotsPerInstance.Length - 1]; + : Info.RequiredShotsPerInstance[^1]; if (shotsFired >= requiredShots) { diff --git a/OpenRA.Mods.Common/Traits/World/WarheadDebugOverlay.cs b/OpenRA.Mods.Common/Traits/World/WarheadDebugOverlay.cs index 54ae5ebcd9..4b37b7f745 100644 --- a/OpenRA.Mods.Common/Traits/World/WarheadDebugOverlay.cs +++ b/OpenRA.Mods.Common/Traits/World/WarheadDebugOverlay.cs @@ -35,7 +35,7 @@ namespace OpenRA.Mods.Common.Traits public readonly Color Color; public int Time; - public WDist OuterRange => Range[Range.Length - 1]; + public WDist OuterRange => Range[^1]; public WHImpact(WPos pos, WDist[] range, int time, Color color) { diff --git a/OpenRA.Mods.Common/Util.cs b/OpenRA.Mods.Common/Util.cs index 025bb1554d..6f98d43f43 100644 --- a/OpenRA.Mods.Common/Util.cs +++ b/OpenRA.Mods.Common/Util.cs @@ -148,7 +148,7 @@ namespace OpenRA.Mods.Common } if (items.Length > 0) - yield return items[items.Length - 1]; + yield return items[^1]; } static IEnumerable Neighbours(CPos c, bool allowDiagonal) diff --git a/OpenRA.Mods.Common/UtilityCommands/ConvertSpriteToPngCommand.cs b/OpenRA.Mods.Common/UtilityCommands/ConvertSpriteToPngCommand.cs index 9d6bb1b886..7779742649 100644 --- a/OpenRA.Mods.Common/UtilityCommands/ConvertSpriteToPngCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/ConvertSpriteToPngCommand.cs @@ -39,9 +39,9 @@ namespace OpenRA.Mods.Common.UtilityCommands if (args.Contains("--noshadow")) { Array.Resize(ref shadowIndex, shadowIndex.Length + 3); - shadowIndex[shadowIndex.Length - 1] = 1; - shadowIndex[shadowIndex.Length - 2] = 3; - shadowIndex[shadowIndex.Length - 3] = 4; + shadowIndex[^1] = 1; + shadowIndex[^2] = 3; + shadowIndex[^3] = 4; } var palette = new ImmutablePalette(args[2], new[] { 0 }, shadowIndex); diff --git a/OpenRA.Mods.Common/Warheads/SpreadDamageWarhead.cs b/OpenRA.Mods.Common/Warheads/SpreadDamageWarhead.cs index 60f90cab02..2b1d491023 100644 --- a/OpenRA.Mods.Common/Warheads/SpreadDamageWarhead.cs +++ b/OpenRA.Mods.Common/Warheads/SpreadDamageWarhead.cs @@ -58,7 +58,7 @@ namespace OpenRA.Mods.Common.Warheads if (debugVis != null && debugVis.CombatGeometry) firedBy.World.WorldActor.Trait().AddImpact(pos, effectiveRange, DebugOverlayColor); - foreach (var victim in firedBy.World.FindActorsOnCircle(pos, effectiveRange[effectiveRange.Length - 1])) + foreach (var victim in firedBy.World.FindActorsOnCircle(pos, effectiveRange[^1])) { if (!IsValidAgainst(victim, firedBy)) continue; @@ -99,7 +99,7 @@ namespace OpenRA.Mods.Common.Warheads } // The range to target is more than the range the warhead covers, so GetDamageFalloff() is going to give us 0 and we're going to do 0 damage anyway, so bail early. - if (falloffDistance > effectiveRange[effectiveRange.Length - 1].Length) + if (falloffDistance > effectiveRange[^1].Length) continue; var localModifiers = args.DamageModifiers.Append(GetDamageFalloff(falloffDistance)); diff --git a/OpenRA.Mods.Common/Widgets/TextNotificationsDisplayWidget.cs b/OpenRA.Mods.Common/Widgets/TextNotificationsDisplayWidget.cs index 3607caba9a..1d0180a1c7 100644 --- a/OpenRA.Mods.Common/Widgets/TextNotificationsDisplayWidget.cs +++ b/OpenRA.Mods.Common/Widgets/TextNotificationsDisplayWidget.cs @@ -61,7 +61,7 @@ namespace OpenRA.Mods.Common.Widgets if (!IsVisible() || Children.Count == 0) return; - var mostRecentMessageOverflows = Bounds.Height < Children[Children.Count - 1].Bounds.Height; + var mostRecentMessageOverflows = Bounds.Height < Children[^1].Bounds.Height; if (mostRecentMessageOverflows && HideOverflow) Game.Renderer.EnableScissor(overflowDrawBounds); @@ -91,7 +91,7 @@ namespace OpenRA.Mods.Common.Widgets foreach (var line in Children) line.Bounds.Y -= notificationWidget.Bounds.Height + ItemSpacing; - var lastLine = Children[Children.Count - 1]; + var lastLine = Children[^1]; notificationWidget.Bounds.Y = lastLine.Bounds.Bottom + ItemSpacing; } @@ -107,7 +107,7 @@ namespace OpenRA.Mods.Common.Widgets if (Children.Count == 0) return; - var mostRecentChild = Children[Children.Count - 1]; + var mostRecentChild = Children[^1]; RemoveChild(mostRecentChild); expirations.RemoveAt(expirations.Count - 1); diff --git a/OpenRA.Mods.D2k/Traits/AttractsWorms.cs b/OpenRA.Mods.D2k/Traits/AttractsWorms.cs index 11e95f1a3c..a304d57114 100644 --- a/OpenRA.Mods.D2k/Traits/AttractsWorms.cs +++ b/OpenRA.Mods.D2k/Traits/AttractsWorms.cs @@ -68,7 +68,7 @@ namespace OpenRA.Mods.D2k.Traits var length = distance.Length; // Actor is too far to hear anything. - if (length > effectiveRange[effectiveRange.Length - 1].Length) + if (length > effectiveRange[^1].Length) return WVec.Zero; var direction = 1024 * distance / length; diff --git a/OpenRA.Utility/Program.cs b/OpenRA.Utility/Program.cs index d064b2b11e..57004bfa1f 100644 --- a/OpenRA.Utility/Program.cs +++ b/OpenRA.Utility/Program.cs @@ -169,7 +169,7 @@ namespace OpenRA return; var args = descParts.Take(descParts.Length - 1).JoinWith(" "); - var desc = descParts[descParts.Length - 1]; + var desc = descParts[^1]; Console.WriteLine($" {key} {args}{Environment.NewLine} {desc}{Environment.NewLine}"); }