浏览代码

Merge pull request #9588 from workgroupengineering/features/NetAnalyzers/CA1304

feat: Enable Rule CA1304
Max Katz 2 年之前
父节点
当前提交
c22464eb7d

+ 2 - 0
.editorconfig

@@ -137,6 +137,8 @@ space_within_single_line_array_initializer_braces = true
 #Net Analyzer
 dotnet_analyzer_diagnostic.category-Performance.severity = none #error - Uncomment when all violations are fixed.
 
+# CA1304: Specify CultureInfo
+dotnet_diagnostic.CA1304.severity = warning
 # CA1802: Use literals where appropriate
 dotnet_diagnostic.CA1802.severity = warning
 # CA1820: Test for empty strings using string length

+ 10 - 8
src/Avalonia.Base/Input/AccessKeyHandler.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Globalization;
 using System.Linq;
 using Avalonia.Interactivity;
 using Avalonia.VisualTree;
@@ -23,7 +24,7 @@ namespace Avalonia.Input
         /// <summary>
         /// The registered access keys.
         /// </summary>
-        private readonly List<Tuple<string, IInputElement>> _registered = new List<Tuple<string, IInputElement>>();
+        private readonly List<(string AccessKey, IInputElement Element)> _registered = new();
 
         /// <summary>
         /// The window to which the handler belongs.
@@ -108,12 +109,12 @@ namespace Avalonia.Input
         {
             var existing = _registered.FirstOrDefault(x => x.Item2 == element);
 
-            if (existing != null)
+            if (existing != default)
             {
                 _registered.Remove(existing);
             }
 
-            _registered.Add(Tuple.Create(accessKey.ToString().ToUpper(), element));
+            _registered.Add((accessKey.ToString().ToUpperInvariant(), element));
         }
 
         /// <summary>
@@ -143,7 +144,7 @@ namespace Avalonia.Input
                 {
                     // TODO: Use FocusScopes to store the current element and restore it when context menu is closed.
                     // Save currently focused input element.
-                    _restoreFocusElement = FocusManager.Instance?.Current;                    
+                    _restoreFocusElement = FocusManager.Instance?.Current;
 
                     // When Alt is pressed without a main menu, or with a closed main menu, show
                     // access key markers in the window (i.e. "_File").
@@ -180,10 +181,11 @@ namespace Avalonia.Input
             {
                 // If any other key is pressed with the Alt key held down, or the main menu is open,
                 // find all controls who have registered that access key.
-                var text = e.Key.ToString().ToUpper();
+                var text = e.Key.ToString();
                 var matches = _registered
-                    .Where(x => x.Item1 == text && ((Visual)x.Item2).IsEffectivelyVisible)
-                    .Select(x => x.Item2);
+                    .Where(x => string.Equals(x.AccessKey, text, StringComparison.OrdinalIgnoreCase)
+                        && x.Element.IsEffectivelyVisible)
+                    .Select(x => x.Element);
 
                 // If the menu is open, only match controls in the menu's visual tree.
                 if (menuIsOpen)
@@ -194,7 +196,7 @@ namespace Avalonia.Input
                 var match = matches.FirstOrDefault();
 
                 // If there was a match, raise the AccessKeyPressed event on it.
-                if (match != null)
+                if (match is not null)
                 {
                     match.RaiseEvent(new RoutedEventArgs(AccessKeyPressedEvent));
                     e.Handled = true;

+ 2 - 1
src/Avalonia.Base/Input/KeyGesture.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Globalization;
 using System.Text;
 using Avalonia.Utilities;
 
@@ -143,7 +144,7 @@ namespace Avalonia.Input
         // TODO: Move that to external key parser
         private static Key ParseKey(string key)
         {
-            if (s_keySynonyms.TryGetValue(key.ToLower(), out Key rv))
+            if (s_keySynonyms.TryGetValue(key.ToLower(CultureInfo.InvariantCulture), out Key rv))
                 return rv;
 
             return EnumHelper.Parse<Key>(key, true);

+ 6 - 5
src/Avalonia.Controls/MenuItemAccessKeyHandler.cs

@@ -14,7 +14,7 @@ namespace Avalonia.Controls
         /// <summary>
         /// The registered access keys.
         /// </summary>
-        private readonly List<Tuple<string, IInputElement>> _registered = new List<Tuple<string, IInputElement>>();
+        private readonly List<(string AccessKey, IInputElement Element)> _registered = new();
 
         /// <summary>
         /// The window to which the handler belongs.
@@ -59,12 +59,12 @@ namespace Avalonia.Controls
         {
             var existing = _registered.FirstOrDefault(x => x.Item2 == element);
 
-            if (existing != null)
+            if (existing != default)
             {
                 _registered.Remove(existing);
             }
 
-            _registered.Add(Tuple.Create(accessKey.ToString().ToUpper(), element));
+            _registered.Add((accessKey.ToString().ToUpperInvariant(), element));
         }
 
         /// <summary>
@@ -88,9 +88,10 @@ namespace Avalonia.Controls
         {
             if (!string.IsNullOrWhiteSpace(e.Text))
             {
-                var text = e.Text.ToUpper();
+                var text = e.Text;
                 var focus = _registered
-                    .FirstOrDefault(x => x.Item1 == text && x.Item2.IsEffectivelyVisible)?.Item2;
+                    .FirstOrDefault(x => string.Equals(x.AccessKey, text, StringComparison.OrdinalIgnoreCase)
+                        && x.Element.IsEffectivelyVisible).Element;
 
                 focus?.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
 

+ 1 - 1
src/Avalonia.Remote.Protocol/MetsysBson.cs

@@ -562,7 +562,7 @@ namespace Metsys.Bson
         {
             if (_string == null && Value != null)
             {
-                _string = BitConverter.ToString(Value).Replace("-", string.Empty).ToLower();
+                _string = BitConverter.ToString(Value).Replace("-", string.Empty).ToLowerInvariant();
             }
 
             return _string;