Update LangVersion to C# 9.

mono was the bottleneck restricting our ability to use a newer C# version. mono 6.12 is currently available. Although poorly documented on their website, this supports C# 9. https://www.mono-project.com/docs/about-mono/versioning/#mono-source-versioning indicates mono 6.12 uses Roslyn 3.9.0. https://github.com/dotnet/roslyn/blob/main/docs/wiki/NuGet-packages.md#versioning indicates Roslyn 3.9.0 supports C# 9.

This unlocks C# 8 and C# 9 features previously unavailable to us.
- https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-80
- https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-9

A newer version of StyleCop is required to avoid rules tripping up on the new syntax. Currently only prerelease versions are available but their use is encouraged https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3420#issuecomment-994899135

Fix style rule violations on existing rules where the newer language version makes some existing casts redundant or allows use of the null coalescing assignment operator.
This commit is contained in:
RoosterDragon
2023-03-26 16:59:58 +01:00
committed by Pavel Penev
parent 9dd4f938da
commit 83561d639d
18 changed files with 42 additions and 38 deletions

View File

@@ -117,7 +117,7 @@ namespace OpenRA.Mods.Common.Activities
if (evaluateNearestMovableCell && destination.HasValue)
{
var movableDestination = mobile.NearestMoveableCell(destination.Value);
destination = mobile.CanEnterCell(movableDestination, check: BlockedByActor.Immovable) ? movableDestination : (CPos?)null;
destination = mobile.CanEnterCell(movableDestination, check: BlockedByActor.Immovable) ? movableDestination : null;
}
// TODO: Change this to BlockedByActor.Stationary after improving the local avoidance behaviour

View File

@@ -492,7 +492,7 @@ namespace OpenRA.Mods.Common.Graphics
});
}).ToArray();
length = length ?? allSprites.Length - start;
length ??= allSprites.Length - start;
if (alpha != null)
{

View File

@@ -68,7 +68,7 @@ namespace OpenRA.Mods.Common.Pathfinder
else
graph = new MapPathGraph(LayerPoolForWorld(world), locomotor, self, world, check, customCost, ignoreActor, laneBias, inReverse);
heuristic = heuristic ?? DefaultCostEstimator(locomotor, target);
heuristic ??= DefaultCostEstimator(locomotor, target);
var search = new PathSearch(graph, heuristic, heuristicWeightPercentage, loc => loc == target, recorder);
AddInitialCells(world, locomotor, froms, customCost, search);

View File

@@ -79,7 +79,7 @@ namespace OpenRA.Mods.Common.Scripting
for (var i = 0; i < actorTypes.Length; i++)
{
var af = actionFunc != null ? (LuaFunction)actionFunc.CopyReference() : null;
var actor = CreateActor(owner, actorTypes[i], false, entryPath[0], entryPath.Length > 1 ? entryPath[1] : (CPos?)null);
var actor = CreateActor(owner, actorTypes[i], false, entryPath[0], entryPath.Length > 1 ? entryPath[1] : null);
actors.Add(actor);
var actionDelay = i * interval;
@@ -118,7 +118,7 @@ namespace OpenRA.Mods.Common.Scripting
public LuaTable ReinforceWithTransport(Player owner, string actorType, string[] cargoTypes, CPos[] entryPath, CPos[] exitPath = null,
LuaFunction actionFunc = null, LuaFunction exitFunc = null, int dropRange = 3)
{
var transport = CreateActor(owner, actorType, true, entryPath[0], entryPath.Length > 1 ? entryPath[1] : (CPos?)null);
var transport = CreateActor(owner, actorType, true, entryPath[0], entryPath.Length > 1 ? entryPath[1] : null);
var cargo = transport.TraitOrDefault<Cargo>();
var passengers = new List<Actor>();

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Scripting
[Desc("Start repairs on this building. `repairer` can be an allied player.")]
public void StartBuildingRepairs(Player repairer = null)
{
repairer = repairer ?? Self.Owner;
repairer ??= Self.Owner;
if (!rb.Repairers.Contains(repairer))
rb.RepairBuilding(Self, repairer);
@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Scripting
[Desc("Stop repairs on this building. `repairer` can be an allied player.")]
public void StopBuildingRepairs(Player repairer = null)
{
repairer = repairer ?? Self.Owner;
repairer ??= Self.Owner;
if (rb.RepairActive && rb.Repairers.Contains(repairer))
rb.RepairBuilding(Self, repairer);

View File

@@ -331,7 +331,7 @@ namespace OpenRA.Mods.Common.Traits
public Actor Unload(Actor self, Actor passenger = null)
{
passenger = passenger ?? cargo.Last();
passenger ??= cargo.Last();
if (!cargo.Remove(passenger))
throw new ArgumentException("Attempted to unload an actor that is not a passenger.");

View File

@@ -71,7 +71,7 @@ namespace OpenRA.Mods.Common.Traits
if (turretFacingInit != null)
{
var facing = turretFacingInit.Value;
return bodyFacing != null ? (Func<WAngle>)(() => bodyFacing() + facing) : () => facing;
return bodyFacing != null ? () => bodyFacing() + facing : () => facing;
}
var dynamicFacingInit = init.GetOrDefault<DynamicTurretFacingInit>(info);

View File

@@ -149,7 +149,7 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
continue;
resolvedSequenceNode.Value.Nodes = MiniYaml.Merge(new[] { resolvedDefaultsNode.Value.Nodes, resolvedSequenceNode.Value.Nodes });
resolvedSequenceNode.Value.Value = resolvedSequenceNode.Value.Value ?? resolvedDefaultsNode.Value.Value;
resolvedSequenceNode.Value.Value ??= resolvedDefaultsNode.Value.Value;
}
}

View File

@@ -101,7 +101,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (button.DisableWorldSounds)
Game.Sound.DisableWorldSounds = true;
widgetArgs = widgetArgs ?? new WidgetArgs();
widgetArgs ??= new WidgetArgs();
widgetArgs.Add("onExit", () =>
{
if (button.HideIngameUI)

View File

@@ -235,7 +235,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
{ "initialMap", modData.MapCache.PickLastModifiedMap(MapVisibility.Lobby) ?? map.Uid },
{ "initialTab", MapClassification.System },
{ "onExit", Game.IsHost ? (Action)UpdateSelectedMap : modData.MapCache.UpdateMaps },
{ "onExit", Game.IsHost ? UpdateSelectedMap : modData.MapCache.UpdateMaps },
{ "onSelect", Game.IsHost ? onSelect : null },
{ "filter", MapVisibility.Lobby },
});