Skip to content

Commit 9409e2d

Browse files
authored
Enable new analyzers CA1862, CA1864 in runtime and fix findings. (#88700)
* Enable new analyzers CA1862, CA1864 in runtime and fix findings
1 parent b4118a6 commit 9409e2d

File tree

16 files changed

+36
-67
lines changed

16 files changed

+36
-67
lines changed

eng/CodeAnalysis.src.globalconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ dotnet_diagnostic.CA1860.severity = warning
474474
# CA1861: Avoid constant arrays as arguments
475475
dotnet_diagnostic.CA1861.severity = warning
476476

477+
# CA1862: Prefer using 'StringComparer'/'StringComparison' to perform case-insensitive string comparisons
478+
dotnet_diagnostic.CA1862.severity = warning
479+
480+
# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
481+
dotnet_diagnostic.CA1864.severity = warning
482+
477483
# CA2000: Dispose objects before losing scope
478484
dotnet_diagnostic.CA2000.severity = none
479485

eng/CodeAnalysis.test.globalconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,12 @@ dotnet_diagnostic.CA1860.severity = none
471471
# CA1861: Avoid constant arrays as arguments
472472
dotnet_diagnostic.CA1861.severity = none
473473

474+
# CA1862: Prefer using 'StringComparer'/'StringComparison' to perform case-insensitive string comparisons
475+
dotnet_diagnostic.CA1862.severity = none
476+
477+
# CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
478+
dotnet_diagnostic.CA1864.severity = none
479+
474480
# CA2000: Dispose objects before losing scope
475481
dotnet_diagnostic.CA2000.severity = none
476482

src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ private string ComputeMangledTypeName(TypeDesc type)
353353
lock (this)
354354
{
355355
// Ensure that name is unique and update our tables accordingly.
356-
if (!_mangledTypeNames.ContainsKey(type))
357-
_mangledTypeNames.Add(type, mangledName);
356+
_mangledTypeNames.TryAdd(type, mangledName);
358357
}
359358

360359
return mangledName;
@@ -386,8 +385,7 @@ public override Utf8String GetMangledMethodName(MethodDesc method)
386385

387386
lock (this)
388387
{
389-
if (!_mangledMethodNames.ContainsKey(method))
390-
_mangledMethodNames.Add(method, utf8MangledName);
388+
_mangledMethodNames.TryAdd(method, utf8MangledName);
391389
}
392390

393391
return utf8MangledName;
@@ -557,8 +555,7 @@ private Utf8String ComputeUnqualifiedMangledMethodName(MethodDesc method)
557555
{
558556
lock (this)
559557
{
560-
if (!_unqualifiedMangledMethodNames.ContainsKey(method))
561-
_unqualifiedMangledMethodNames.Add(method, utf8MangledName);
558+
_unqualifiedMangledMethodNames.TryAdd(method, utf8MangledName);
562559
}
563560
}
564561

@@ -622,8 +619,7 @@ private Utf8String ComputeMangledFieldName(FieldDesc field)
622619

623620
lock (this)
624621
{
625-
if (!_mangledFieldNames.ContainsKey(field))
626-
_mangledFieldNames.Add(field, utf8MangledName);
622+
_mangledFieldNames.TryAdd(field, utf8MangledName);
627623
}
628624

629625
return utf8MangledName;
@@ -644,8 +640,7 @@ public override string GetMangledStringName(string literal)
644640

645641
lock (this)
646642
{
647-
if (!_mangledStringLiterals.ContainsKey(literal))
648-
_mangledStringLiterals.Add(literal, mangledName);
643+
_mangledStringLiterals.TryAdd(literal, mangledName);
649644
}
650645

651646
return mangledName;

src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileData.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public IBCProfileData(MibcConfig config, bool partialNGen, IEnumerable<MethodPro
5151
MethodProfileData[] dataArray = methodData.ToArray();
5252
foreach (MethodProfileData data in dataArray)
5353
{
54-
if (!_methodData.ContainsKey(data.Method))
55-
{
56-
_methodData.Add(data.Method, data);
57-
}
54+
_methodData.TryAdd(data.Method, data);
5855
}
5956
_partialNGen = partialNGen;
6057
_config = config;

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/AggregationManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ private void PublishedInstrument(Instrument instrument, MeterListener _)
129129
if (state != null)
130130
{
131131
_beginInstrumentMeasurements(instrument);
132-
132+
#pragma warning disable CA1864 // Prefer the 'IDictionary.TryAdd(TKey, TValue)' method. IDictionary.TryAdd() is not available in one of the builds
133133
if (!_instruments.ContainsKey(instrument))
134+
#pragma warning restore CA1864
134135
{
135136
// This has side effects that prompt MeasurementsCompleted
136137
// to be called if this is called multiple times on an

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int
522522
}
523523
else
524524
{
525-
if (processInfos.ContainsKey(processInfo.ProcessId))
525+
if (!processInfos.TryAdd(processInfo.ProcessId, processInfo))
526526
{
527527
// We've found two entries in the perfcounters that claim to be the
528528
// same process. We throw an exception. Is this really going to be
@@ -549,7 +549,6 @@ private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int
549549
}
550550
}
551551
processInfo.ProcessName = instanceName.ToString();
552-
processInfos.Add(processInfo.ProcessId, processInfo);
553552
}
554553
}
555554
}

src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD/ADDNLinkedAttrSet.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,10 @@ private bool MoveNextForeign(ref bool outerNeedToRetry)
667667
if (null == foreignSid.sidIssuerName)
668668
{
669669
// create and return the unknown principal if it is not yet present in usersVisited
670-
if (!_usersVisited.ContainsKey(foreignSid.name))
670+
if (_usersVisited.TryAdd(foreignSid.name, true))
671671
{
672672
byte[] sid = Utils.ConvertNativeSidToByteArray(foreignSid.pSid);
673673
UnknownPrincipal unknownPrincipal = UnknownPrincipal.CreateUnknownPrincipal(_storeCtx.OwningContext, sid, foreignSid.name);
674-
_usersVisited.Add(foreignSid.name, true);
675674
this.current = null;
676675
_currentForeignDE = null;
677676
_currentForeignPrincipal = unknownPrincipal;

src/libraries/System.Private.Xml.Linq/src/System/Xml/Schema/XNodeValidator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,7 @@ private void ReplaceSchemaInfo(XObject o, XmlSchemaInfo schemaInfo)
157157
XmlSchemaInfo? si = o.Annotation<XmlSchemaInfo>();
158158
if (si != null)
159159
{
160-
if (!schemaInfos.ContainsKey(si))
161-
{
162-
schemaInfos.Add(si, si);
163-
}
160+
schemaInfos.TryAdd(si, si);
164161
o.RemoveAnnotations<XmlSchemaInfo>();
165162
}
166163
if (!schemaInfos.TryGetValue(schemaInfo, out si))

src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParser.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,17 +1229,11 @@ private void ParseEntityDecl()
12291229

12301230
if (isParamEntity)
12311231
{
1232-
if (!_schemaInfo.ParameterEntities.ContainsKey(entityName))
1233-
{
1234-
_schemaInfo.ParameterEntities.Add(entityName, entity);
1235-
}
1232+
_schemaInfo.ParameterEntities.TryAdd(entityName, entity);
12361233
}
12371234
else
12381235
{
1239-
if (!_schemaInfo.GeneralEntities.ContainsKey(entityName))
1240-
{
1241-
_schemaInfo.GeneralEntities.Add(entityName, entity);
1242-
}
1236+
_schemaInfo.GeneralEntities.TryAdd(entityName, entity);
12431237
}
12441238
entity.DeclaredInExternal = !ParsingInternalSubset;
12451239
entity.ParsingInProgress = true;

src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParserAsync.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -861,17 +861,11 @@ private async Task ParseEntityDeclAsync()
861861

862862
if (isParamEntity)
863863
{
864-
if (!_schemaInfo.ParameterEntities.ContainsKey(entityName))
865-
{
866-
_schemaInfo.ParameterEntities.Add(entityName, entity);
867-
}
864+
_schemaInfo.ParameterEntities.TryAdd(entityName, entity);
868865
}
869866
else
870867
{
871-
if (!_schemaInfo.GeneralEntities.ContainsKey(entityName))
872-
{
873-
_schemaInfo.GeneralEntities.Add(entityName, entity);
874-
}
868+
_schemaInfo.GeneralEntities.TryAdd(entityName, entity);
875869
}
876870
entity.DeclaredInExternal = !ParsingInternalSubset;
877871
entity.ParsingInProgress = true;

0 commit comments

Comments
 (0)