diff --git a/.editorconfig b/.editorconfig index edb4d5c30e..8f7998107a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -153,7 +153,7 @@ dotnet_diagnostic.IDE0018.severity = warning # Use pattern matching to avoid 'as' followed by a 'null' check. dotnet_diagnostic.IDE0019.severity = warning -# Use pattern matching to avoid 'is' check followed by a cast. +# Use pattern matching to avoid 'is' check followed by a cast (with variable). dotnet_diagnostic.IDE0020.severity = warning # Collection initialization can be simplified. @@ -177,6 +177,9 @@ dotnet_diagnostic.IDE0034.severity = warning # Modifiers are not ordered. dotnet_diagnostic.IDE0036.severity = warning +# Use pattern matching to avoid 'is' check followed by a cast (without variable). +dotnet_diagnostic.IDE0038.severity = warning + # Use local function instead of lambda. dotnet_diagnostic.IDE0039.severity = warning diff --git a/OpenRA.Game/CPos.cs b/OpenRA.Game/CPos.cs index aa4df1ddca..396c150810 100644 --- a/OpenRA.Game/CPos.cs +++ b/OpenRA.Game/CPos.cs @@ -56,7 +56,7 @@ namespace OpenRA public override int GetHashCode() { return Bits.GetHashCode(); } public bool Equals(CPos other) { return Bits == other.Bits; } - public override bool Equals(object obj) { return obj is CPos && Equals((CPos)obj); } + public override bool Equals(object obj) { return obj is CPos cell && Equals(cell); } public override string ToString() { diff --git a/OpenRA.Game/CVec.cs b/OpenRA.Game/CVec.cs index 3ab56ba1a6..d4f13b5f04 100644 --- a/OpenRA.Game/CVec.cs +++ b/OpenRA.Game/CVec.cs @@ -55,7 +55,7 @@ namespace OpenRA public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } public bool Equals(CVec other) { return other == this; } - public override bool Equals(object obj) { return obj is CVec && Equals((CVec)obj); } + public override bool Equals(object obj) { return obj is CVec vec && Equals(vec); } public override string ToString() { return X + "," + Y; } diff --git a/OpenRA.Game/MPos.cs b/OpenRA.Game/MPos.cs index 462e593026..9793f0926e 100644 --- a/OpenRA.Game/MPos.cs +++ b/OpenRA.Game/MPos.cs @@ -27,7 +27,7 @@ namespace OpenRA public override int GetHashCode() { return U.GetHashCode() ^ V.GetHashCode(); } public bool Equals(MPos other) { return other == this; } - public override bool Equals(object obj) { return obj is MPos && Equals((MPos)obj); } + public override bool Equals(object obj) { return obj is MPos uv && Equals(uv); } public MPos Clamp(Rectangle r) { @@ -88,7 +88,7 @@ namespace OpenRA public override int GetHashCode() { return U.GetHashCode() ^ V.GetHashCode(); } public bool Equals(PPos other) { return other == this; } - public override bool Equals(object obj) { return obj is PPos && Equals((PPos)obj); } + public override bool Equals(object obj) { return obj is PPos puv && Equals(puv); } public override string ToString() { return U + "," + V; } } diff --git a/OpenRA.Game/Primitives/Int32Matrix4x4.cs b/OpenRA.Game/Primitives/Int32Matrix4x4.cs index a43ca2b99b..de2f7cf32d 100644 --- a/OpenRA.Game/Primitives/Int32Matrix4x4.cs +++ b/OpenRA.Game/Primitives/Int32Matrix4x4.cs @@ -58,7 +58,7 @@ namespace OpenRA public override int GetHashCode() { return M11 ^ M22 ^ M33 ^ M44; } public bool Equals(Int32Matrix4x4 other) { return other == this; } - public override bool Equals(object obj) { return obj is Int32Matrix4x4 && Equals((Int32Matrix4x4)obj); } + public override bool Equals(object obj) { return obj is Int32Matrix4x4 matrix && Equals(matrix); } public override string ToString() { diff --git a/OpenRA.Game/Primitives/LongBitSet.cs b/OpenRA.Game/Primitives/LongBitSet.cs index c18aeb6391..a1fe2a55b7 100644 --- a/OpenRA.Game/Primitives/LongBitSet.cs +++ b/OpenRA.Game/Primitives/LongBitSet.cs @@ -116,7 +116,7 @@ namespace OpenRA.Primitives public static bool operator !=(LongBitSet me, LongBitSet other) { return !(me == other); } public bool Equals(LongBitSet other) { return other == this; } - public override bool Equals(object obj) { return obj is LongBitSet && Equals((LongBitSet)obj); } + public override bool Equals(object obj) { return obj is LongBitSet set && Equals(set); } public override int GetHashCode() { return bits.GetHashCode(); } public bool IsEmpty => bits == 0; diff --git a/OpenRA.Game/Primitives/float2.cs b/OpenRA.Game/Primitives/float2.cs index 666024b874..edaa0f45e5 100644 --- a/OpenRA.Game/Primitives/float2.cs +++ b/OpenRA.Game/Primitives/float2.cs @@ -72,7 +72,7 @@ namespace OpenRA public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } public bool Equals(float2 other) { return this == other; } - public override bool Equals(object obj) { return obj is float2 && Equals((float2)obj); } + public override bool Equals(object obj) { return obj is float2 vec && Equals(vec); } public override string ToString() { return X + "," + Y; } diff --git a/OpenRA.Game/Primitives/int2.cs b/OpenRA.Game/Primitives/int2.cs index 1f822ae4f0..e1f86655c1 100644 --- a/OpenRA.Game/Primitives/int2.cs +++ b/OpenRA.Game/Primitives/int2.cs @@ -37,7 +37,7 @@ namespace OpenRA public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } public bool Equals(int2 other) { return this == other; } - public override bool Equals(object obj) { return obj is int2 && Equals((int2)obj); } + public override bool Equals(object obj) { return obj is int2 vec && Equals(vec); } public override string ToString() { return X + "," + Y; } diff --git a/OpenRA.Game/Scripting/ScriptTypes.cs b/OpenRA.Game/Scripting/ScriptTypes.cs index 26bfaabdff..062464e0f0 100644 --- a/OpenRA.Game/Scripting/ScriptTypes.cs +++ b/OpenRA.Game/Scripting/ScriptTypes.cs @@ -109,10 +109,9 @@ namespace OpenRA.Scripting } // Translate LuaTable -> object[] - if (value is LuaTable && t.IsArray) + if (value is LuaTable table && t.IsArray) { var innerType = t.GetElementType(); - var table = (LuaTable)value; var array = Array.CreateInstance(innerType, table.Count); var i = 0; @@ -149,23 +148,25 @@ namespace OpenRA.Scripting public static LuaValue ToLuaValue(this object obj, ScriptContext context) { - if (obj is LuaValue) - return (LuaValue)obj; + { + if (obj is LuaValue v) + return v; - if (obj == null) - return LuaNil.Instance; + if (obj == null) + return LuaNil.Instance; - if (obj is double) - return (double)obj; + if (obj is double d) + return d; - if (obj is int) - return (int)obj; + if (obj is int i) + return i; - if (obj is bool) - return (bool)obj; + if (obj is bool b) + return b; - if (obj is string) - return (string)obj; + if (obj is string s) + return s; + } if (obj is IScriptBindable) { diff --git a/OpenRA.Game/WAngle.cs b/OpenRA.Game/WAngle.cs index d8ced13279..c4f6378e52 100644 --- a/OpenRA.Game/WAngle.cs +++ b/OpenRA.Game/WAngle.cs @@ -44,7 +44,7 @@ namespace OpenRA public override int GetHashCode() { return Angle.GetHashCode(); } public bool Equals(WAngle other) { return other == this; } - public override bool Equals(object obj) { return obj is WAngle && Equals((WAngle)obj); } + public override bool Equals(object obj) { return obj is WAngle angle && Equals(angle); } public int Facing => Angle / 4; diff --git a/OpenRA.Game/WDist.cs b/OpenRA.Game/WDist.cs index 5c7a5abd52..691542c41c 100644 --- a/OpenRA.Game/WDist.cs +++ b/OpenRA.Game/WDist.cs @@ -93,7 +93,7 @@ namespace OpenRA public override int GetHashCode() { return Length.GetHashCode(); } public bool Equals(WDist other) { return other == this; } - public override bool Equals(object obj) { return obj is WDist && Equals((WDist)obj); } + public override bool Equals(object obj) { return obj is WDist dist && Equals(dist); } public int CompareTo(object obj) { diff --git a/OpenRA.Game/WRot.cs b/OpenRA.Game/WRot.cs index 120af3a8ec..c0aae12777 100644 --- a/OpenRA.Game/WRot.cs +++ b/OpenRA.Game/WRot.cs @@ -190,7 +190,7 @@ namespace OpenRA public override int GetHashCode() { return Roll.GetHashCode() ^ Pitch.GetHashCode() ^ Yaw.GetHashCode(); } public bool Equals(WRot other) { return other == this; } - public override bool Equals(object obj) { return obj is WRot && Equals((WRot)obj); } + public override bool Equals(object obj) { return obj is WRot rot && Equals(rot); } public override string ToString() { return Roll + "," + Pitch + "," + Yaw; } diff --git a/OpenRA.Game/WVec.cs b/OpenRA.Game/WVec.cs index 380c70fdd5..7e0006bdf8 100644 --- a/OpenRA.Game/WVec.cs +++ b/OpenRA.Game/WVec.cs @@ -102,7 +102,7 @@ namespace OpenRA public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); } public bool Equals(WVec other) { return other == this; } - public override bool Equals(object obj) { return obj is WVec && Equals((WVec)obj); } + public override bool Equals(object obj) { return obj is WVec vec && Equals(vec); } public override string ToString() { return X + "," + Y + "," + Z; } diff --git a/OpenRA.Game/World.cs b/OpenRA.Game/World.cs index b1a6187e39..d284303c83 100644 --- a/OpenRA.Game/World.cs +++ b/OpenRA.Game/World.cs @@ -637,7 +637,7 @@ namespace OpenRA public override int GetHashCode() { return Actor.GetHashCode() ^ Trait.GetHashCode(); } public bool Equals(TraitPair other) { return this == other; } - public override bool Equals(object obj) { return obj is TraitPair && Equals((TraitPair)obj); } + public override bool Equals(object obj) { return obj is TraitPair pair && Equals(pair); } public override string ToString() { return Actor.Info.Name + "->" + Trait.GetType().Name; } } diff --git a/OpenRA.Mods.Common/Traits/Cloak.cs b/OpenRA.Mods.Common/Traits/Cloak.cs index 93c27bb616..26ff74f53d 100644 --- a/OpenRA.Mods.Common/Traits/Cloak.cs +++ b/OpenRA.Mods.Common/Traits/Cloak.cs @@ -163,7 +163,7 @@ namespace OpenRA.Mods.Common.Traits if (palette == null) return r; else - return r.Select(a => !a.IsDecoration && a is IPalettedRenderable ? ((IPalettedRenderable)a).WithPalette(palette) : a); + return r.Select(a => !a.IsDecoration && a is IPalettedRenderable pr ? pr.WithPalette(palette) : a); } else return SpriteRenderable.None;