From 65aa403d51449b262a2cf650aed66ab9f37fb26d Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 4 Apr 2025 10:33:51 +0200 Subject: [PATCH 1/2] Fix CA1310 in the codebase --- .../Generated/WinRT/ComWrappersSupport.cs | 2 +- .../Generated/WinRT/TypeExtensions.cs | 16 +++++++--------- .../Windows/Controls/ItemContainerGenerator.cs | 2 +- .../System/Windows/Documents/TextStore.cs | 2 +- .../Markup/Primitives/ElementMarkupObject.cs | 9 +++++---- .../Automation/HwndProxyElementProvider.cs | 4 +++- .../MS/Internal/AutomationProxies/Misc.cs | 4 +++- 7 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/ComWrappersSupport.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/ComWrappersSupport.cs index e9cdea23698..9caab759b70 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/ComWrappersSupport.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/ComWrappersSupport.cs @@ -153,7 +153,7 @@ public static (InspectableInfo inspectableInfo, List interfac Type type = obj.GetType(); - if (type.FullName.StartsWith("ABI.")) + if (type.FullName.StartsWith("ABI.", StringComparison.Ordinal)) { type = Projections.FindCustomPublicTypeForAbiType(type) ?? type.Assembly.GetType(type.FullName.Substring("ABI.".Length)) ?? type; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/TypeExtensions.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/TypeExtensions.cs index 992017e5342..bc0428294fa 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/TypeExtensions.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/TypeExtensions.cs @@ -5,9 +5,10 @@ namespace WinRT { - internal static class TypeExtensions { + private const string NamespacePrefix = "MS.Internal.WindowsRuntime."; + public static Type FindHelperType(this Type type) { if (typeof(Exception).IsAssignableFrom(type)) @@ -19,12 +20,14 @@ public static Type FindHelperType(this Type type) { return customMapping; } - var helper = $"ABI.{type.FullName}"; + + string helper = $"ABI.{type.FullName}"; string helperTypeName2 = $"MS.Internal.WindowsRuntime.ABI.{type.FullName}"; - if (type.FullName.StartsWith("MS.Internal.WindowsRuntime.")) + if (type.FullName.StartsWith(NamespacePrefix, StringComparison.Ordinal)) { helper = $"MS.Internal.WindowsRuntime.ABI.{RemoveNamespacePrefix(type.FullName)}"; } + return Type.GetType(helper) ?? Type.GetType(helperTypeName2); } @@ -72,12 +75,7 @@ public static bool IsDelegate(this Type type) public static string RemoveNamespacePrefix(string ns) { - const string NamespacePrefix = "MS.Internal.WindowsRuntime."; - if (ns.StartsWith(NamespacePrefix)) - { - return ns.Substring(NamespacePrefix.Length); - } - return ns; + return ns.StartsWith(NamespacePrefix, StringComparison.Ordinal) ? ns.Substring(NamespacePrefix.Length) : ns; } } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs index 1e1b1dce9b4..aef71c59e1c 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs @@ -1115,7 +1115,7 @@ private void FormatCollectionChangedSource(int level, object source, bool? isLik const string PublicKeyToken = "PublicKeyToken="; string aqn = sourceType.AssemblyQualifiedName; - int index = aqn.LastIndexOf(PublicKeyToken); + int index = aqn.LastIndexOf(PublicKeyToken, StringComparison.Ordinal); if (index >= 0) { ReadOnlySpan token = aqn.AsSpan(index + PublicKeyToken.Length); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs index 95e27646679..f57449df45c 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs @@ -4829,7 +4829,7 @@ private static string DisplayType(object o) } string name = t.ToString(); - isWPFControl = name.StartsWith("System.Windows.Controls."); + isWPFControl = name.StartsWith("System.Windows.Controls.", StringComparison.Ordinal); if (isWPFControl) { name = name.Substring(24); // 24 == length of "s.w.c." diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/ElementMarkupObject.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/ElementMarkupObject.cs index af8688be38c..b5266bde22a 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/ElementMarkupObject.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/ElementMarkupObject.cs @@ -495,15 +495,16 @@ public override IEnumerable Items Type keyType = GetDictionaryKeyType(dictionary); // Attempt to emit the contents of the dictionary in a more deterministic - // order. This is not guarenteed to be deterministic because it uses ToString - // which is not guarenteed to be unique for each value. Keys with non-unique + // order. This is not guaranteed to be deterministic because it uses ToString + // which is not guaranteed to be unique for each value. Keys with non-unique // ToString() values will not be emitted deterministically. DictionaryEntry[] entries = new DictionaryEntry[dictionary.Count]; dictionary.CopyTo(entries, 0); - Array.Sort(entries, delegate(DictionaryEntry one, DictionaryEntry two) + Array.Sort(entries, static (DictionaryEntry one, DictionaryEntry two) => { - return String.Compare(one.Key.ToString(), two.Key.ToString()); + return string.Compare(one.Key.ToString(), two.Key.ToString(), StringComparison.Ordinal); }); + foreach (DictionaryEntry entry in entries) { ElementMarkupObject subItem diff --git a/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs b/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs index f18d5a17fd7..db8b4e67493 100644 --- a/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs +++ b/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs @@ -1892,12 +1892,14 @@ private static bool IsProgmanWindow(NativeMethods.HWND hwnd) { while (hwnd != NativeMethods.HWND.NULL) { - if (ProxyManager.GetClassName(hwnd).CompareTo("Progman") == 0) + if (ProxyManager.GetClassName(hwnd).Equals("Progman", StringComparison.Ordinal)) { return true; } + hwnd = SafeNativeMethods.GetAncestor(hwnd, SafeNativeMethods.GA_PARENT); } + return false; } diff --git a/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs b/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs index 9d9c9e91a15..a17fb873dc2 100644 --- a/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs +++ b/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs @@ -864,12 +864,14 @@ internal static bool IsProgmanWindow(IntPtr hwnd) { while (hwnd != IntPtr.Zero) { - if (GetClassName(hwnd).CompareTo("Progman") == 0) + if (GetClassName(hwnd).Equals("Progman", StringComparison.Ordinal)) { return true; } + hwnd = NativeMethodsSetLastError.GetAncestor(hwnd, NativeMethods.GA_PARENT); } + return false; } From df06cef0bed87ed5758f7d077fecdcea1920d742 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 4 Apr 2025 10:34:00 +0200 Subject: [PATCH 2/2] Adjust .editorconfig --- src/Microsoft.DotNet.Wpf/src/.editorconfig | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/.editorconfig b/src/Microsoft.DotNet.Wpf/src/.editorconfig index ebe14fe879a..37377963d70 100644 --- a/src/Microsoft.DotNet.Wpf/src/.editorconfig +++ b/src/Microsoft.DotNet.Wpf/src/.editorconfig @@ -38,9 +38,6 @@ dotnet_diagnostic.CA1067.severity = suggestion # CA1070: Do not declare event fields as virtual dotnet_diagnostic.CA1070.severity = suggestion -# CA1310: Specify StringComparison for correctness -dotnet_diagnostic.CA1310.severity = suggestion - # CA1419: Provide a parameterless constructor that is as visible as the containing type for concrete types derived from 'System.Runtime.InteropServices.SafeHandle' dotnet_diagnostic.CA1419.severity = suggestion