Skip to content

Commit 0e106fa

Browse files
authoredFeb 29, 2024
1 parent f8ca532 commit 0e106fa

File tree

15 files changed

+26
-65
lines changed

15 files changed

+26
-65
lines changed
 

‎.globalconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ dotnet_diagnostic.CA1846.severity = warning
510510
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1847
511511
dotnet_diagnostic.CA1847.severity = warning
512512

513+
# CA1868: Unnecessary call to 'Contains' for sets
514+
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1868
515+
dotnet_diagnostic.CA1868.severity = warning
516+
513517
# CA2000: Dispose objects before losing scope
514518
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000
515519
dotnet_diagnostic.CA2000.severity = none

‎src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionOperations.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,8 @@ internal IEnumerable<PSObject> QuerySession(
535535
{
536536
if (this.curCimSessionsById.ContainsKey(id))
537537
{
538-
if (!sessionIds.Contains(id))
538+
if (sessionIds.Add(id))
539539
{
540-
sessionIds.Add(id);
541540
sessions.Add(this.curCimSessionsById[id].GetPSObject());
542541
}
543542
}

‎src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-SddlString.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ private static List<string> GetApplicableAccessRights(int accessMask, AccessRigh
8181
foreach (string memberName in Enum.GetNames(accessRightType))
8282
{
8383
int memberValue = (int)Enum.Parse(accessRightType, memberName);
84-
if (!foundAccessRightValues.Contains(memberValue))
84+
if (foundAccessRightValues.Add(memberValue))
8585
{
86-
foundAccessRightValues.Add(memberValue);
8786
if ((accessMask & memberValue) == memberValue)
8887
{
8988
foundAccessRightNames.Add(memberName);

‎src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,11 +1419,7 @@ private static void ValidatePropertyNames(IList<string> names)
14191419
{
14201420
if (!string.IsNullOrEmpty(currentHeader))
14211421
{
1422-
if (!headers.Contains(currentHeader))
1423-
{
1424-
headers.Add(currentHeader);
1425-
}
1426-
else
1422+
if (!headers.Add(currentHeader))
14271423
{
14281424
// throw a terminating error as there are duplicate headers in the input.
14291425
string memberAlreadyPresentMsg =

‎src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,8 @@ private void ProcessTypeFiles()
792792

793793
if (ShouldProcess(formattedTarget, action))
794794
{
795-
if (!fullFileNameHash.Contains(resolvedPath))
795+
if (fullFileNameHash.Add(resolvedPath))
796796
{
797-
fullFileNameHash.Add(resolvedPath);
798797
newTypes.Add(new SessionStateTypeEntry(prependPathTotal[i]));
799798
}
800799
}
@@ -806,9 +805,8 @@ private void ProcessTypeFiles()
806805
if (entry.FileName != null)
807806
{
808807
string resolvedPath = ModuleCmdletBase.ResolveRootedFilePath(entry.FileName, Context) ?? entry.FileName;
809-
if (!fullFileNameHash.Contains(resolvedPath))
808+
if (fullFileNameHash.Add(resolvedPath))
810809
{
811-
fullFileNameHash.Add(resolvedPath);
812810
newTypes.Add(entry);
813811
}
814812
}
@@ -825,9 +823,8 @@ private void ProcessTypeFiles()
825823

826824
if (ShouldProcess(formattedTarget, action))
827825
{
828-
if (!fullFileNameHash.Contains(resolvedPath))
826+
if (fullFileNameHash.Add(resolvedPath))
829827
{
830-
fullFileNameHash.Add(resolvedPath);
831828
newTypes.Add(new SessionStateTypeEntry(appendPathTotalItem));
832829
}
833830
}
@@ -971,9 +968,8 @@ protected override void ProcessRecord()
971968

972969
if (ShouldProcess(formattedTarget, action))
973970
{
974-
if (!fullFileNameHash.Contains(appendPathTotalItem))
971+
if (fullFileNameHash.Add(appendPathTotalItem))
975972
{
976-
fullFileNameHash.Add(appendPathTotalItem);
977973
newFormats.Add(new SessionStateFormatEntry(appendPathTotalItem));
978974
}
979975
}

‎src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,7 @@ internal static AppliesTo GetAllApplicableTypes(TypeInfoDataBase db, AppliesTo a
601601
// if it is a type reference, just add the type name
602602
if (r is TypeReference tr)
603603
{
604-
if (!allTypes.Contains(tr.name))
605-
allTypes.Add(tr.name);
604+
allTypes.Add(tr.name);
606605
}
607606
else
608607
{
@@ -619,8 +618,7 @@ internal static AppliesTo GetAllApplicableTypes(TypeInfoDataBase db, AppliesTo a
619618
// we found the group, go over it
620619
foreach (TypeReference x in tgd.typeReferenceList)
621620
{
622-
if (!allTypes.Contains(x.name))
623-
allTypes.Add(x.name);
621+
allTypes.Add(x.name);
624622
}
625623
}
626624
}

‎src/System.Management.Automation/engine/CommandDiscovery.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,10 +1151,8 @@ internal void RegisterLookupCommandInfoAction(string currentAction, string comma
11511151
case "ActivePostCommand": currentActionSet = _activePostCommand; break;
11521152
}
11531153

1154-
if (currentActionSet.Contains(command))
1154+
if (!currentActionSet.Add(command))
11551155
throw new InvalidOperationException();
1156-
else
1157-
currentActionSet.Add(command);
11581156
}
11591157

11601158
internal void UnregisterLookupCommandInfoAction(string currentAction, string command)
@@ -1168,8 +1166,7 @@ internal void UnregisterLookupCommandInfoAction(string currentAction, string com
11681166
case "ActivePostCommand": currentActionSet = _activePostCommand; break;
11691167
}
11701168

1171-
if (currentActionSet.Contains(command))
1172-
currentActionSet.Remove(command);
1169+
currentActionSet.Remove(command);
11731170
}
11741171

11751172
private readonly HashSet<string> _activePreLookup = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

‎src/System.Management.Automation/engine/InitialSessionState.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,14 +1569,10 @@ public static InitialSessionState CreateDefault()
15691569
string assembly = ss.Assemblies[i].FileName;
15701570
if (!string.IsNullOrEmpty(assembly))
15711571
{
1572-
if (assemblyList.Contains(assembly))
1572+
if (!assemblyList.Add(assembly))
15731573
{
15741574
ss.Assemblies.RemoveItem(i);
15751575
}
1576-
else
1577-
{
1578-
assemblyList.Add(assembly);
1579-
}
15801576
}
15811577
}
15821578

‎src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,8 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(List<string> modulePat
10471047
PSModuleInfo module = CreateModuleInfoForGetModule(resolvedModulePath, refresh);
10481048
if (module != null)
10491049
{
1050-
if (!modules.Contains(resolvedModulePath))
1050+
if (modules.Add(resolvedModulePath))
10511051
{
1052-
modules.Add(resolvedModulePath);
10531052
yield return module;
10541053
}
10551054
}
@@ -1078,9 +1077,8 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(List<string> modulePat
10781077
foundModule = true;
10791078
// We need to list all versions of the module.
10801079
string subModulePath = Path.GetDirectoryName(file);
1081-
if (!modules.Contains(subModulePath))
1080+
if (modules.Add(subModulePath))
10821081
{
1083-
modules.Add(subModulePath);
10841082
yield return module;
10851083
}
10861084
}

‎src/System.Management.Automation/engine/parser/PSType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,9 +1321,8 @@ internal static Assembly DefineTypes(Parser parser, Ast rootAst, TypeDefinitionA
13211321
foreach (var typeDefinitionAst in typeDefinitions)
13221322
{
13231323
var typeName = GetClassNameInAssembly(typeDefinitionAst);
1324-
if (!definedTypes.Contains(typeName))
1324+
if (definedTypes.Add(typeName))
13251325
{
1326-
definedTypes.Add(typeName);
13271326
if ((typeDefinitionAst.TypeAttributes & TypeAttributes.Class) == TypeAttributes.Class)
13281327
{
13291328
defineTypeHelpers.Add(new DefineTypeHelper(parser, module, typeDefinitionAst, typeName));

‎src/System.Management.Automation/engine/parser/SemanticChecks.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,13 @@ private void CheckForDuplicateParameters(ReadOnlyCollection<ParameterAst> parame
9090
foreach (var parameter in parameters)
9191
{
9292
string parameterName = parameter.Name.VariablePath.UserPath;
93-
if (parametersSet.Contains(parameterName))
93+
if (!parametersSet.Add(parameterName))
9494
{
9595
_parser.ReportError(parameter.Name.Extent,
9696
nameof(ParserStrings.DuplicateFormalParameter),
9797
ParserStrings.DuplicateFormalParameter,
9898
parameterName);
9999
}
100-
else
101-
{
102-
parametersSet.Add(parameterName);
103-
}
104100

105101
var voidConstraint =
106102
parameter.Attributes.OfType<TypeConstraintAst>().FirstOrDefault(static t => typeof(void) == t.TypeName.GetReflectionType());
@@ -242,7 +238,7 @@ public override AstVisitAction VisitAttribute(AttributeAst attributeAst)
242238
foreach (var namedArg in attributeAst.NamedArguments)
243239
{
244240
string name = namedArg.ArgumentName;
245-
if (names.Contains(name))
241+
if (!names.Add(name))
246242
{
247243
_parser.ReportError(namedArg.Extent,
248244
nameof(ParserStrings.DuplicateNamedArgument),
@@ -251,8 +247,6 @@ public override AstVisitAction VisitAttribute(AttributeAst attributeAst)
251247
}
252248
else
253249
{
254-
names.Add(name);
255-
256250
if (!namedArg.ExpressionOmitted && !IsValidAttributeArgument(namedArg.Argument, constantValueVisitor))
257251
{
258252
var error = GetNonConstantAttributeArgErrorExpr(constantValueVisitor);
@@ -1124,7 +1118,7 @@ public override AstVisitAction VisitHashtable(HashtableAst hashtableAst)
11241118
if (keyStrAst != null)
11251119
{
11261120
var keyStr = keyStrAst.Value.ToString();
1127-
if (keys.Contains(keyStr))
1121+
if (!keys.Add(keyStr))
11281122
{
11291123
string errorId;
11301124
string errorMsg;
@@ -1141,10 +1135,6 @@ public override AstVisitAction VisitHashtable(HashtableAst hashtableAst)
11411135

11421136
_parser.ReportError(entry.Item1.Extent, errorId, errorMsg, keyStr);
11431137
}
1144-
else
1145-
{
1146-
keys.Add(keyStr);
1147-
}
11481138
}
11491139
}
11501140

‎src/System.Management.Automation/engine/remoting/client/RemotingProtocol2.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,10 +797,7 @@ private void HandleReadyForDisconnect(object sender, EventArgs args)
797797
return;
798798
}
799799

800-
if (_preparingForDisconnectList.Contains(bcmdTM))
801-
{
802-
_preparingForDisconnectList.Remove(bcmdTM);
803-
}
800+
_preparingForDisconnectList.Remove(bcmdTM);
804801

805802
if (_preparingForDisconnectList.Count == 0)
806803
{

‎src/System.Management.Automation/help/CommandHelpProvider.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,7 @@ internal override IEnumerable<HelpInfo> SearchHelp(HelpRequest helpRequest, bool
10811081
{
10821082
// this command is not visible to the user (from CommandOrigin) so
10831083
// dont show help topic for it.
1084-
if (!hiddenCommands.Contains(helpName))
1085-
{
1086-
hiddenCommands.Add(helpName);
1087-
}
1084+
hiddenCommands.Add(helpName);
10881085

10891086
continue;
10901087
}

‎src/System.Management.Automation/help/HelpFileHelpProvider.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,7 @@ private Collection<string> FilterToLatestModuleVersion(Collection<string> filesM
170170
{
171171
string fileName = Path.GetFileName(file);
172172

173-
if (!fileNameHash.Contains(fileName))
174-
{
175-
fileNameHash.Add(fileName);
176-
}
177-
else
173+
if (!fileNameHash.Add(fileName))
178174
{
179175
// If the file need to be removed, add it to matchedFilesToRemove, if not already present.
180176
if (!matchedFilesToRemove.Contains(file))

‎src/System.Management.Automation/security/CatalogHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ internal static void ProcessFileToBeAddedInCatalogDefinitionFile(FileInfo fileTo
220220
relativePath = fileToHash.Name;
221221
}
222222

223-
if (!relativePaths.Contains(relativePath))
223+
if (relativePaths.Add(relativePath))
224224
{
225-
relativePaths.Add(relativePath);
226225
if (fileToHash.Length != 0)
227226
{
228227
cdfFilesContent += "<HASH>" + fileToHash.FullName + "=" + fileToHash.FullName + Environment.NewLine;

0 commit comments

Comments
 (0)