diff --git a/eng/CodeAnalysis.src.globalconfig b/eng/CodeAnalysis.src.globalconfig index f7b055d9c75030..36a3ff9f2eb143 100644 --- a/eng/CodeAnalysis.src.globalconfig +++ b/eng/CodeAnalysis.src.globalconfig @@ -474,6 +474,12 @@ dotnet_diagnostic.CA1860.severity = warning # CA1861: Avoid constant arrays as arguments dotnet_diagnostic.CA1861.severity = warning +# CA1862: Prefer using 'StringComparer'/'StringComparison' to perform case-insensitive string comparisons +dotnet_diagnostic.CA1862.severity = warning + +# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method +dotnet_diagnostic.CA1864.severity = warning + # CA2000: Dispose objects before losing scope dotnet_diagnostic.CA2000.severity = none diff --git a/eng/CodeAnalysis.test.globalconfig b/eng/CodeAnalysis.test.globalconfig index fa3d2593e530f9..d884d9f4efc1eb 100644 --- a/eng/CodeAnalysis.test.globalconfig +++ b/eng/CodeAnalysis.test.globalconfig @@ -471,6 +471,12 @@ dotnet_diagnostic.CA1860.severity = none # CA1861: Avoid constant arrays as arguments dotnet_diagnostic.CA1861.severity = none +# CA1862: Prefer using 'StringComparer'/'StringComparison' to perform case-insensitive string comparisons +dotnet_diagnostic.CA1862.severity = none + +# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method +dotnet_diagnostic.CA1864.severity = none + # CA2000: Dispose objects before losing scope dotnet_diagnostic.CA2000.severity = none diff --git a/src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs b/src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs index d5818b5aecc428..787767ed0d0774 100644 --- a/src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs +++ b/src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs @@ -353,8 +353,7 @@ private string ComputeMangledTypeName(TypeDesc type) lock (this) { // Ensure that name is unique and update our tables accordingly. - if (!_mangledTypeNames.ContainsKey(type)) - _mangledTypeNames.Add(type, mangledName); + _mangledTypeNames.TryAdd(type, mangledName); } return mangledName; @@ -386,8 +385,7 @@ public override Utf8String GetMangledMethodName(MethodDesc method) lock (this) { - if (!_mangledMethodNames.ContainsKey(method)) - _mangledMethodNames.Add(method, utf8MangledName); + _mangledMethodNames.TryAdd(method, utf8MangledName); } return utf8MangledName; @@ -557,8 +555,7 @@ private Utf8String ComputeUnqualifiedMangledMethodName(MethodDesc method) { lock (this) { - if (!_unqualifiedMangledMethodNames.ContainsKey(method)) - _unqualifiedMangledMethodNames.Add(method, utf8MangledName); + _unqualifiedMangledMethodNames.TryAdd(method, utf8MangledName); } } @@ -622,8 +619,7 @@ private Utf8String ComputeMangledFieldName(FieldDesc field) lock (this) { - if (!_mangledFieldNames.ContainsKey(field)) - _mangledFieldNames.Add(field, utf8MangledName); + _mangledFieldNames.TryAdd(field, utf8MangledName); } return utf8MangledName; @@ -644,8 +640,7 @@ public override string GetMangledStringName(string literal) lock (this) { - if (!_mangledStringLiterals.ContainsKey(literal)) - _mangledStringLiterals.Add(literal, mangledName); + _mangledStringLiterals.TryAdd(literal, mangledName); } return mangledName; diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileData.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileData.cs index a36d77e8fe6526..5fabd83920ea29 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileData.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileData.cs @@ -51,10 +51,7 @@ public IBCProfileData(MibcConfig config, bool partialNGen, IEnumerable(); if (si != null) { - if (!schemaInfos.ContainsKey(si)) - { - schemaInfos.Add(si, si); - } + schemaInfos.TryAdd(si, si); o.RemoveAnnotations(); } if (!schemaInfos.TryGetValue(schemaInfo, out si)) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParser.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParser.cs index 4b9d94743da4d5..96cd5df6ac1c9f 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParser.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParser.cs @@ -1229,17 +1229,11 @@ private void ParseEntityDecl() if (isParamEntity) { - if (!_schemaInfo.ParameterEntities.ContainsKey(entityName)) - { - _schemaInfo.ParameterEntities.Add(entityName, entity); - } + _schemaInfo.ParameterEntities.TryAdd(entityName, entity); } else { - if (!_schemaInfo.GeneralEntities.ContainsKey(entityName)) - { - _schemaInfo.GeneralEntities.Add(entityName, entity); - } + _schemaInfo.GeneralEntities.TryAdd(entityName, entity); } entity.DeclaredInExternal = !ParsingInternalSubset; entity.ParsingInProgress = true; diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParserAsync.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParserAsync.cs index 8bc28f86cb469b..fb13f2f8fce681 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParserAsync.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParserAsync.cs @@ -861,17 +861,11 @@ private async Task ParseEntityDeclAsync() if (isParamEntity) { - if (!_schemaInfo.ParameterEntities.ContainsKey(entityName)) - { - _schemaInfo.ParameterEntities.Add(entityName, entity); - } + _schemaInfo.ParameterEntities.TryAdd(entityName, entity); } else { - if (!_schemaInfo.GeneralEntities.ContainsKey(entityName)) - { - _schemaInfo.GeneralEntities.Add(entityName, entity); - } + _schemaInfo.GeneralEntities.TryAdd(entityName, entity); } entity.DeclaredInExternal = !ParsingInternalSubset; entity.ParsingInProgress = true; diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaCollectionCompiler.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaCollectionCompiler.cs index 48ff221c96fdd7..c4cdfe3e3acac5 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaCollectionCompiler.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaCollectionCompiler.cs @@ -305,10 +305,7 @@ private void Output(SchemaInfo schemaInfo) SchemaNotation no = new SchemaNotation(notation.QualifiedName); no.SystemLiteral = notation.System; no.Pubid = notation.Public; - if (!schemaInfo.Notations.ContainsKey(no.Name.Name)) - { - schemaInfo.Notations.Add(no.Name.Name, no); - } + schemaInfo.Notations.TryAdd(no.Name.Name, no); } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaInfo.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaInfo.cs index 7fc94acbb4355d..df934c11576176 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaInfo.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaInfo.cs @@ -299,10 +299,7 @@ internal void Add(SchemaInfo sinfo, ValidationEventHandler? eventhandler) foreach (string tns in sinfo.TargetNamespaces.Keys) { - if (!_targetNamespaces.ContainsKey(tns)) - { - _targetNamespaces.Add(tns, true); - } + _targetNamespaces.TryAdd(tns, true); } foreach (KeyValuePair entry in sinfo._elementDecls) @@ -321,17 +318,11 @@ internal void Add(SchemaInfo sinfo, ValidationEventHandler? eventhandler) } foreach (SchemaAttDef attdef in sinfo.AttributeDecls.Values) { - if (!_attributeDecls.ContainsKey(attdef.Name)) - { - _attributeDecls.Add(attdef.Name, attdef); - } + _attributeDecls.TryAdd(attdef.Name, attdef); } foreach (SchemaNotation notation in sinfo.Notations.Values) { - if (!Notations.ContainsKey(notation.Name.Name)) - { - Notations.Add(notation.Name.Name, notation); - } + Notations.TryAdd(notation.Name.Name, notation); } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaSetCompiler.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaSetCompiler.cs index 9ede49c1e73039..6fe5a25c62c9ee 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaSetCompiler.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaSetCompiler.cs @@ -167,10 +167,7 @@ private void Output(SchemaInfo schemaInfo) SchemaNotation no = new SchemaNotation(notation!.QualifiedName); no.SystemLiteral = notation.System; no.Pubid = notation.Public; - if (!schemaInfo.Notations.ContainsKey(no.Name.Name)) - { - schemaInfo.Notations.Add(no.Name.Name, no); - } + schemaInfo.Notations.TryAdd(no.Name.Name, no); } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XdrBuilder.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XdrBuilder.cs index e37bbe1715e3ff..2c268337367605 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XdrBuilder.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XdrBuilder.cs @@ -1051,11 +1051,7 @@ private static void XDR_BuildAttributeType_Name(XdrBuilder builder, object obj, // Global AttributeTypes are URN qualified so that we can look them up across schemas. qname = new XmlQualifiedName(qname.Name, builder._TargetNamespace); builder._AttributeDef._AttDef.Name = qname; - if (!builder._SchemaInfo.AttributeDecls.ContainsKey(qname)) - { - builder._SchemaInfo.AttributeDecls.Add(qname, builder._AttributeDef._AttDef); - } - else + if (!builder._SchemaInfo.AttributeDecls.TryAdd(qname, builder._AttributeDef._AttDef)) { builder.SendValidationEvent(SR.Sch_DupAttribute, XmlQualifiedName.ToString(qname.Name, prefix)); } diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ComputeWasmPublishAssets.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ComputeWasmPublishAssets.cs index 8a9bcc500dadc1..0a9521f9b27953 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ComputeWasmPublishAssets.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ComputeWasmPublishAssets.cs @@ -577,6 +577,7 @@ private void GroupResolvedFilesToPublish( foreach (var candidate in resolvedFilesToPublish) { +#pragma warning disable CA1864 // Prefer the 'IDictionary.TryAdd(TKey, TValue)' method. Dictionary.TryAdd() not available in .Net framework. if (AssetsComputingHelper.ShouldFilterCandidate(candidate, TimeZoneSupport, InvariantGlobalization, CopySymbols, customIcuCandidateFilename, EnableThreads, EmitSourceMap, out var reason)) { Log.LogMessage(MessageImportance.Low, "Skipping asset '{0}' because '{1}'", candidate.ItemSpec, reason); @@ -654,6 +655,7 @@ private void GroupResolvedFilesToPublish( } continue; } +#pragma warning restore CA1864 } } diff --git a/src/tools/illink/src/tlens/TLens.Analyzers/UnnecessaryFieldsAssignmentAnalyzer.cs b/src/tools/illink/src/tlens/TLens.Analyzers/UnnecessaryFieldsAssignmentAnalyzer.cs index bf88c682542e0a..812a874bd9e36d 100644 --- a/src/tools/illink/src/tlens/TLens.Analyzers/UnnecessaryFieldsAssignmentAnalyzer.cs +++ b/src/tools/illink/src/tlens/TLens.Analyzers/UnnecessaryFieldsAssignmentAnalyzer.cs @@ -72,9 +72,7 @@ protected override void ProcessMethod (MethodDefinition method) methods.Add (method); } - if (!fields.ContainsKey (field)) - fields.Add (field, access); - else + if (!fields.TryAdd (field, access)) fields[field] |= access; } }