From c3fc7b63f25d42e606c48d5cd196e54e38f2162f Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Tue, 26 Nov 2024 20:44:17 +0000 Subject: [PATCH 1/2] Fix `IDE0004` for object comparison --- .../src/System/Reflection/CustomAttributeData.cs | 2 -- .../System.Private.CoreLib/src/System/RuntimeType.cs | 8 -------- .../src/System/String.Comparison.cs | 8 ++++---- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs index 1ef9923364e096..9c86b4d74457c0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs @@ -76,8 +76,6 @@ public override string ToString() return vsb.ToString(); } - public override int GetHashCode() => base.GetHashCode(); - public override bool Equals(object? obj) => obj == (object)this; #endregion #region Public Members diff --git a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs index 2b30db584b6590..35816273175c7b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs @@ -34,12 +34,6 @@ public override RuntimeTypeHandle TypeHandle public object Clone() => this; - public override bool Equals(object? obj) - { - // ComObjects are identified by the instance of the Type object and not the TypeHandle. - return obj == (object)this; - } - public override int GetArrayRank() { if (!IsArrayImpl()) @@ -165,8 +159,6 @@ public override Type GetEnumUnderlyingType() return Enum.InternalGetUnderlyingType(this); } - public override int GetHashCode() => RuntimeHelpers.GetHashCode(this); - internal RuntimeModule GetRuntimeModule() => RuntimeTypeHandle.GetModule(this); protected override TypeCode GetTypeCodeImpl() diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs b/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs index 9eacfefab5d1eb..5e82309ef4249b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs @@ -539,7 +539,7 @@ public bool EndsWith(string value, StringComparison comparisonType) { ArgumentNullException.ThrowIfNull(value); - if ((object)this == (object)value) + if (ReferenceEquals(this, value)) { CheckStringComparison(comparisonType); return true; @@ -580,7 +580,7 @@ public bool EndsWith(string value, bool ignoreCase, CultureInfo? culture) { ArgumentNullException.ThrowIfNull(value); - if ((object)this == (object)value) + if (ReferenceEquals(this, value)) { return true; } @@ -1108,7 +1108,7 @@ public bool StartsWith(string value, StringComparison comparisonType) { ArgumentNullException.ThrowIfNull(value); - if ((object)this == (object)value) + if (ReferenceEquals(this, value)) { CheckStringComparison(comparisonType); return true; @@ -1158,7 +1158,7 @@ public bool StartsWith(string value, bool ignoreCase, CultureInfo? culture) { ArgumentNullException.ThrowIfNull(value); - if ((object)this == (object)value) + if (ReferenceEquals(this, value)) { return true; } From 04d3df57dbccf982c6ace7342758bed05fcbd16c Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Tue, 26 Nov 2024 22:11:02 +0000 Subject: [PATCH 2/2] revert `src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs` --- .../System.Private.CoreLib/src/System/RuntimeType.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs index 35816273175c7b..2b30db584b6590 100644 --- a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs @@ -34,6 +34,12 @@ public override RuntimeTypeHandle TypeHandle public object Clone() => this; + public override bool Equals(object? obj) + { + // ComObjects are identified by the instance of the Type object and not the TypeHandle. + return obj == (object)this; + } + public override int GetArrayRank() { if (!IsArrayImpl()) @@ -159,6 +165,8 @@ public override Type GetEnumUnderlyingType() return Enum.InternalGetUnderlyingType(this); } + public override int GetHashCode() => RuntimeHelpers.GetHashCode(this); + internal RuntimeModule GetRuntimeModule() => RuntimeTypeHandle.GetModule(this); protected override TypeCode GetTypeCodeImpl()