From bc491144e29d633cd270ffe8d06fd138c23c241c Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Mon, 29 Apr 2024 09:56:18 -0500 Subject: [PATCH 1/5] enforce CA1854 --- src/Core/src/.editorconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Core/src/.editorconfig b/src/Core/src/.editorconfig index 55bb5ffa7e5e..71129dd827b7 100644 --- a/src/Core/src/.editorconfig +++ b/src/Core/src/.editorconfig @@ -14,6 +14,7 @@ dotnet_diagnostic.CA1834.severity = error dotnet_diagnostic.CA1845.severity = error dotnet_diagnostic.CA1846.severity = error dotnet_diagnostic.CA1847.severity = error +dotnet_diagnostic.CA1854.severity = error dotnet_diagnostic.CA1859.severity = error dotnet_diagnostic.CA1860.severity = error dotnet_diagnostic.CA1864.severity = error From 38f2d5f2b623077516ceb2964d2dcc45b0aecad3 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Mon, 29 Apr 2024 10:08:37 -0500 Subject: [PATCH 2/5] fix CA1854 --- .../CompiledConverters/FontSizeTypeConverter.cs | 4 ++-- .../src/Build.Tasks/CreateObjectVisitor.cs | 15 ++++++++------- .../src/Build.Tasks/MethodDefinitionExtensions.cs | 6 +++--- .../src/Build.Tasks/SetPropertiesVisitor.cs | 8 ++++---- .../Shell/iOS/ShellSectionRootRenderer.cs | 3 +-- src/Controls/src/Core/Internals/NameScope.cs | 4 ++-- src/Controls/src/Core/MessagingCenter.cs | 14 +++++++------- src/Controls/src/Core/ResourceDictionary.cs | 8 ++++---- src/Controls/src/Core/ResourcesExtensions.cs | 4 ++-- src/Controls/src/Xaml/ApplyPropertiesVisitor.cs | 10 +++++----- src/Controls/src/Xaml/CreateValuesVisitor.cs | 8 ++++---- .../src/Xaml/SimplifyTypeExtensionVisitor.cs | 4 ++-- .../Core.UnitTests/DependencyResolutionTests.cs | 8 ++++---- .../Android/LocalizedDigitsKeyListener.cs | 7 ++++--- .../src/Permissions/Permissions.android.cs | 4 ++-- src/Essentials/src/Preferences/Preferences.uwp.cs | 3 +-- .../src/DeviceTests/xUnitCustomizations.cs | 4 ++-- 17 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs b/src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs index b6f89e55d531..545ece0efa04 100644 --- a/src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs +++ b/src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs @@ -28,9 +28,9 @@ public IEnumerable ConvertFromString(string value, ILContext contex //Device.GetNamedSize(namedSize, targetObject.GetType()) yield return Instruction.Create(OpCodes.Ldc_I4, (int)namedSize); var parent = node.Parent as IElementNode; - if (parent != null && context.Variables.ContainsKey(parent)) + if (parent != null && context.Variables.TryGetValue(parent, out VariableDefinition parentValue)) { - yield return Instruction.Create(OpCodes.Ldloc, context.Variables[parent]); + yield return Instruction.Create(OpCodes.Ldloc, parentValue); yield return Instruction.Create(OpCodes.Callvirt, module.ImportMethodReference( context.Cache, module.TypeSystem.Object, diff --git a/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs b/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs index 543a8f4bbe80..a0ece490f3ee 100644 --- a/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs +++ b/src/Controls/src/Build.Tasks/CreateObjectVisitor.cs @@ -96,8 +96,9 @@ public void Visit(ElementNode node, INode parentNode) MethodDefinition factoryMethodInfo = null; MethodDefinition parameterizedCtorInfo = null; MethodDefinition ctorInfo = null; - - if (node.Properties.ContainsKey(XmlName.xArguments) && !node.Properties.ContainsKey(XmlName.xFactoryMethod)) + + INode factoryMethodNode = null; + if (node.Properties.ContainsKey(XmlName.xArguments) && !node.Properties.TryGetValue(XmlName.xFactoryMethod, out factoryMethodNode)) { factoryCtorInfo = typedef.AllMethods(Context.Cache).FirstOrDefault(md => md.methodDef.IsConstructor && !md.methodDef.IsStatic && @@ -107,9 +108,9 @@ public void Visit(ElementNode node, INode parentNode) if (!typedef.IsValueType) //for ctor'ing typedefs, we first have to ldloca before the params Context.IL.Append(PushCtorXArguments(factoryCtorInfo.ResolveGenericParameters(typeref, Module), node)); } - else if (node.Properties.ContainsKey(XmlName.xFactoryMethod)) + else if (factoryMethodNode != null) { - var factoryMethod = (string)(node.Properties[XmlName.xFactoryMethod] as ValueNode).Value; + var factoryMethod = (string)(factoryMethodNode as ValueNode).Value; factoryMethodInfo = typedef.AllMethods(Context.Cache).FirstOrDefault(md => !md.methodDef.IsConstructor && md.methodDef.Name == factoryMethod && md.methodDef.IsStatic && @@ -317,14 +318,14 @@ IEnumerable PushCtorArguments(MethodReference ctorinfo, ElementNode IEnumerable PushCtorXArguments(MethodReference factoryCtorInfo, ElementNode enode) { - if (!enode.Properties.ContainsKey(XmlName.xArguments)) + if (!enode.Properties.TryGetValue(XmlName.xArguments, out INode value)) yield break; var arguments = new List(); - var node = enode.Properties[XmlName.xArguments] as ElementNode; + var node = value as ElementNode; if (node != null) arguments.Add(node); - var list = enode.Properties[XmlName.xArguments] as ListNode; + var list = value as ListNode; if (list != null) { foreach (var n in list.CollectionItems) diff --git a/src/Controls/src/Build.Tasks/MethodDefinitionExtensions.cs b/src/Controls/src/Build.Tasks/MethodDefinitionExtensions.cs index 8c44720d5363..432b6c22c3d7 100644 --- a/src/Controls/src/Build.Tasks/MethodDefinitionExtensions.cs +++ b/src/Controls/src/Build.Tasks/MethodDefinitionExtensions.cs @@ -8,15 +8,15 @@ static class MethodDefinitionExtensions { public static bool MatchXArguments(this MethodDefinition methodDef, ElementNode enode, TypeReference declaringTypeRef, ModuleDefinition module, ILContext context) { - if (!enode.Properties.ContainsKey(XmlName.xArguments)) + if (!enode.Properties.TryGetValue(XmlName.xArguments, out INode value)) return !methodDef.HasParameters; var arguments = new List(); - var node = enode.Properties[XmlName.xArguments] as ElementNode; + var node = value as ElementNode; if (node != null) arguments.Add(node); - var list = enode.Properties[XmlName.xArguments] as ListNode; + var list = value as ListNode; if (list != null) foreach (var n in list.CollectionItems) arguments.Add(n); diff --git a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs index 15f021d26bda..1f119173dc47 100644 --- a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs +++ b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs @@ -1499,9 +1499,9 @@ static bool CanAddToResourceDictionary(VariableDefinition parent, TypeReference && !collectionType.InheritsFromOrImplements(context.Cache, context.Module.ImportReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "ResourceDictionary")))) return false; - if (node.Properties.ContainsKey(XmlName.xKey)) + if (node.Properties.TryGetValue(XmlName.xKey, out INode nodePropertyValue)) { - var valueNode = node.Properties[XmlName.xKey] as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null); + var valueNode = nodePropertyValue as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null); var key = (valueNode).Value as string; var names = context.Cache.GetResourceNamesInUse(parent); if (names.Contains(key)) @@ -1552,10 +1552,10 @@ static IEnumerable AddToResourceDictionary(VariableDefinition paren { var module = context.Body.Method.Module; - if (node.Properties.ContainsKey(XmlName.xKey)) + if (node.Properties.TryGetValue(XmlName.xKey, out INode nodePropertyValue)) { var names = context.Cache.GetResourceNamesInUse(parent); - var valueNode = node.Properties[XmlName.xKey] as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null); + var valueNode = nodePropertyValue as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null); var key = (valueNode).Value as string; names.Add(key); diff --git a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRootRenderer.cs b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRootRenderer.cs index 8468cf0f16d6..90336b44e067 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRootRenderer.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/Shell/iOS/ShellSectionRootRenderer.cs @@ -350,10 +350,9 @@ protected virtual void OnShellSectionPropertyChanged(object sender, PropertyChan _currentContent = newContent; _currentIndex = newIndex; - if (!_renderers.ContainsKey(newContent)) + if (!_renderers.TryGetValue(newContent, out var currentRenderer)) return; - var currentRenderer = _renderers[newContent]; _isAnimatingOut = oldRenderer; _pageAnimation?.StopAnimation(true); _pageAnimation = null; diff --git a/src/Controls/src/Core/Internals/NameScope.cs b/src/Controls/src/Core/Internals/NameScope.cs index d86031f406c6..f11fb29d6629 100644 --- a/src/Controls/src/Core/Internals/NameScope.cs +++ b/src/Controls/src/Core/Internals/NameScope.cs @@ -52,10 +52,10 @@ void INameScope.UnregisterName(string name) if (name == "") throw new ArgumentException("name was provided as empty string.", nameof(name)); - if (!_names.ContainsKey(name)) + if (!_names.TryGetValue(name, out var nameValue)) throw new ArgumentException("name provided had not been registered.", nameof(name)); - _values.Remove(_names[name]); + _values.Remove(nameValue); _names.Remove(name); } } diff --git a/src/Controls/src/Core/MessagingCenter.cs b/src/Controls/src/Core/MessagingCenter.cs index bb14b25aaba7..a674fd0b020a 100644 --- a/src/Controls/src/Core/MessagingCenter.cs +++ b/src/Controls/src/Core/MessagingCenter.cs @@ -202,9 +202,9 @@ void InnerSend(string message, Type senderType, Type argType, object sender, obj if (message == null) throw new ArgumentNullException(nameof(message)); var key = new Sender(message, senderType, argType); - if (!_subscriptions.ContainsKey(key)) + if (!_subscriptions.TryGetValue(key, out List subcriptions)) return; - List subcriptions = _subscriptions[key]; + if (subcriptions == null || !subcriptions.Any()) return; // should not be reachable @@ -229,9 +229,9 @@ void InnerSubscribe(object subscriber, string message, Type senderType, Type arg throw new ArgumentNullException(nameof(message)); var key = new Sender(message, senderType, argType); var value = new Subscription(subscriber, target, methodInfo, filter); - if (_subscriptions.ContainsKey(key)) + if (_subscriptions.TryGetValue(key, out List subscriptions)) { - _subscriptions[key].Add(value); + subscriptions.Add(value); } else { @@ -248,10 +248,10 @@ void InnerUnsubscribe(string message, Type senderType, Type argType, object subs throw new ArgumentNullException(nameof(message)); var key = new Sender(message, senderType, argType); - if (!_subscriptions.ContainsKey(key)) + if (!_subscriptions.TryGetValue(key, out List subscriptions)) return; - _subscriptions[key].RemoveAll(sub => sub.CanBeRemoved() || sub.Subscriber.Target == subscriber); - if (!_subscriptions[key].Any()) + subscriptions.RemoveAll(sub => sub.CanBeRemoved() || sub.Subscriber.Target == subscriber); + if (!subscriptions.Any()) _subscriptions.Remove(key); } diff --git a/src/Controls/src/Core/ResourceDictionary.cs b/src/Controls/src/Core/ResourceDictionary.cs index 5cb81399d0df..72bcd2dec286 100644 --- a/src/Controls/src/Core/ResourceDictionary.cs +++ b/src/Controls/src/Core/ResourceDictionary.cs @@ -190,10 +190,10 @@ public object this[string index] { get { - if (_innerDictionary.ContainsKey(index)) - return _innerDictionary[index]; - if (_mergedInstance != null && _mergedInstance.ContainsKey(index)) - return _mergedInstance[index]; + if (_innerDictionary.TryGetValue(index, out var innerValue)) + return innerValue; + if (_mergedInstance != null && _mergedInstance.TryGetValue(index, out var mergedValue)) + return mergedValue; if (_mergedDictionaries != null) { var dictionaries = (ObservableCollection)MergedDictionaries; diff --git a/src/Controls/src/Core/ResourcesExtensions.cs b/src/Controls/src/Core/ResourcesExtensions.cs index 32673eaba4c6..c2393d2bf40f 100644 --- a/src/Controls/src/Core/ResourcesExtensions.cs +++ b/src/Controls/src/Core/ResourcesExtensions.cs @@ -35,11 +35,11 @@ public static IEnumerable> GetMergedResources(this { resources = resources ?? new Dictionary(8, StringComparer.Ordinal); foreach (KeyValuePair res in app.SystemResources) - if (!resources.ContainsKey(res.Key)) + if (!resources.TryGetValue(res.Key, out var resourceValue)) resources.Add(res.Key, res.Value); else if (res.Key.StartsWith(Style.StyleClassPrefix, StringComparison.Ordinal)) { - var mergedClassStyles = new List