From 4021ce09e6d1f148c3cac660bd9d3068ad3f3b04 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sun, 2 Feb 2025 17:53:27 +0000 Subject: [PATCH] Simplify conditional operator --- .../src/System/Diagnostics/StackFrameHelper.cs | 2 +- .../src/System/Collections/Hashtable.cs | 2 +- src/libraries/System.Private.CoreLib/src/System/Convert.cs | 4 ++-- src/libraries/System.Private.CoreLib/src/System/Delegate.cs | 4 ++-- .../System.Private.CoreLib/src/System/MulticastDelegate.cs | 4 ++-- .../src/System/Reflection/Assembly.cs | 2 +- .../src/System/Reflection/ConstructorInfo.cs | 2 +- .../src/System/Reflection/EventInfo.cs | 2 +- .../src/System/Reflection/FieldInfo.cs | 2 +- .../src/System/Reflection/MemberInfo.cs | 2 +- .../src/System/Reflection/MethodBase.cs | 2 +- .../src/System/Reflection/MethodInfo.cs | 2 +- .../System.Private.CoreLib/src/System/Reflection/Module.cs | 2 +- .../src/System/Reflection/PropertyInfo.cs | 2 +- src/libraries/System.Private.CoreLib/src/System/Type.cs | 6 +++--- src/libraries/System.Private.CoreLib/src/System/Version.cs | 2 +- 16 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs b/src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs index f10af9c2347126..1abd2c7caf15c5 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs @@ -177,7 +177,7 @@ internal void InitializeSourceInfo(bool fNeedFileInfo, Exception? exception) public bool IsLastFrameFromForeignExceptionStackTrace(int i) { - return (rgiLastFrameFromForeignExceptionStackTrace == null) ? false : rgiLastFrameFromForeignExceptionStackTrace[i]; + return rgiLastFrameFromForeignExceptionStackTrace != null && rgiLastFrameFromForeignExceptionStackTrace[i]; } public int GetNumberOfFrames() { return iFrameCount; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Hashtable.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Hashtable.cs index 15883fa2eee2d7..bae03e1780937f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Hashtable.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Hashtable.cs @@ -798,7 +798,7 @@ protected virtual bool KeyEquals(object? item, object key) if (_keycomparer != null) return _keycomparer.Equals(item, key); - return item == null ? false : item.Equals(key); + return item != null && item.Equals(key); } // Returns a collection representing the keys of this hashtable. The order diff --git a/src/libraries/System.Private.CoreLib/src/System/Convert.cs b/src/libraries/System.Private.CoreLib/src/System/Convert.cs index 62e4cd8412ab90..0e128a3e7a5c95 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Convert.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Convert.cs @@ -314,12 +314,12 @@ internal static object DefaultToType(IConvertible value, Type targetType, IForma // Conversions to Boolean public static bool ToBoolean([NotNullWhen(true)] object? value) { - return value == null ? false : ((IConvertible)value).ToBoolean(null); + return value != null && ((IConvertible)value).ToBoolean(null); } public static bool ToBoolean([NotNullWhen(true)] object? value, IFormatProvider? provider) { - return value == null ? false : ((IConvertible)value).ToBoolean(provider); + return value != null && ((IConvertible)value).ToBoolean(provider); } public static bool ToBoolean(bool value) diff --git a/src/libraries/System.Private.CoreLib/src/System/Delegate.cs b/src/libraries/System.Private.CoreLib/src/System/Delegate.cs index 4675761d5d6e2f..6efa9f48554fad 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Delegate.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Delegate.cs @@ -193,7 +193,7 @@ public bool MoveNext() return d1 is null; } - return ReferenceEquals(d2, d1) ? true : d2.Equals((object?)d1); + return ReferenceEquals(d2, d1) || d2.Equals(d1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -206,7 +206,7 @@ public bool MoveNext() return d1 is not null; } - return ReferenceEquals(d2, d1) ? false : !d2.Equals(d1); + return !ReferenceEquals(d2, d1) && !d2.Equals(d1); } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/MulticastDelegate.cs b/src/libraries/System.Private.CoreLib/src/System/MulticastDelegate.cs index 207cbf6874b93c..ac06921d387ad4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/MulticastDelegate.cs +++ b/src/libraries/System.Private.CoreLib/src/System/MulticastDelegate.cs @@ -35,7 +35,7 @@ protected MulticastDelegate([DynamicallyAccessedMembers(DynamicallyAccessedMembe return d1 is null; } - return ReferenceEquals(d2, d1) ? true : d2.Equals((object?)d1); + return ReferenceEquals(d2, d1) || d2.Equals(d1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -50,7 +50,7 @@ protected MulticastDelegate([DynamicallyAccessedMembers(DynamicallyAccessedMembe return d1 is not null; } - return ReferenceEquals(d2, d1) ? false : !d2.Equals(d1); + return !ReferenceEquals(d2, d1) && !d2.Equals(d1); } } #pragma warning restore CS0660, CS0661 diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs index 019ded05b4a89d..a1878467759b3d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs @@ -196,7 +196,7 @@ public override string ToString() return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(Assembly? left, Assembly? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs index c4619fdead1218..a680267e160719 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInfo.cs @@ -37,7 +37,7 @@ protected ConstructorInfo() { } return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(ConstructorInfo? left, ConstructorInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/EventInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/EventInfo.cs index 4aec52e2be955a..72867205759963 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/EventInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/EventInfo.cs @@ -92,7 +92,7 @@ public virtual void RemoveEventHandler(object? target, Delegate? handler) return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(EventInfo? left, EventInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldInfo.cs index 374c0ba5aeeb13..73573345c2a217 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldInfo.cs @@ -56,7 +56,7 @@ protected FieldInfo() { } return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(FieldInfo? left, FieldInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MemberInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MemberInfo.cs index 89cc304a0ef42d..2bb7eb46c83f2b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MemberInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MemberInfo.cs @@ -59,7 +59,7 @@ public virtual Module Module return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(MemberInfo? left, MemberInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs index 72be9d4897d582..d638a995b50f52 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs @@ -81,7 +81,7 @@ this is ConstructorInfo && return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(MethodBase? left, MethodBase? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs index 5e8afa6d70e30d..00fe2589f818e2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs @@ -54,7 +54,7 @@ protected MethodInfo() { } return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(MethodInfo? left, MethodInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs index 0b87f2335ec293..982a7e6d687d28 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs @@ -161,7 +161,7 @@ public virtual Type[] FindTypes(TypeFilter? filter, object? filterCriteria) return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(Module? left, Module? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/PropertyInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/PropertyInfo.cs index 24c1196334a8b0..fa9c8c2cecec9d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/PropertyInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/PropertyInfo.cs @@ -75,7 +75,7 @@ protected PropertyInfo() { } return true; } - return (left is null) ? false : left.Equals(right); + return left is not null && left.Equals(right); } public static bool operator !=(PropertyInfo? left, PropertyInfo? right) => !(left == right); diff --git a/src/libraries/System.Private.CoreLib/src/System/Type.cs b/src/libraries/System.Private.CoreLib/src/System/Type.cs index 6947b0e5f04981..8562821454873b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Type.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Type.cs @@ -599,7 +599,7 @@ protected virtual TypeCode GetTypeCodeImpl() public virtual InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) => throw new NotSupportedException(SR.NotSupported_SubclassOverride); - public virtual bool IsInstanceOfType([NotNullWhen(true)] object? o) => o == null ? false : IsAssignableFrom(o.GetType()); + public virtual bool IsInstanceOfType([NotNullWhen(true)] object? o) => o != null && IsAssignableFrom(o.GetType()); public virtual bool IsEquivalentTo([NotNullWhen(true)] Type? other) => this == other; [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2085:UnrecognizedReflectionPattern", @@ -680,7 +680,7 @@ internal string FormatTypeName() public override string ToString() => "Type: " + Name; // Why do we add the "Type: " prefix? - public override bool Equals(object? o) => o == null ? false : Equals(o as Type); + public override bool Equals(object? o) => o != null && Equals(o as Type); public override int GetHashCode() { Type systemType = UnderlyingSystemType; @@ -688,7 +688,7 @@ public override int GetHashCode() return systemType.GetHashCode(); return base.GetHashCode(); } - public virtual bool Equals(Type? o) => o == null ? false : ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType); + public virtual bool Equals(Type? o) => o != null && ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType); [Intrinsic] public static bool operator ==(Type? left, Type? right) diff --git a/src/libraries/System.Private.CoreLib/src/System/Version.cs b/src/libraries/System.Private.CoreLib/src/System/Version.cs index 9aeac15161b873..9f2ee94f70770c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Version.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Version.cs @@ -451,7 +451,7 @@ private static bool TryParseComponent(ReadOnlySpan component, stri } // Quick reference equality test prior to calling the virtual Equality - return ReferenceEquals(v2, v1) ? true : v2.Equals(v1); + return ReferenceEquals(v2, v1) || v2.Equals(v1); } public static bool operator !=(Version? v1, Version? v2) => !(v1 == v2);