Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ dotnet_diagnostic.CA1846.severity = warning
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1847
dotnet_diagnostic.CA1847.severity = warning

# CA1868: Unnecessary call to 'Contains' for sets
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1868
dotnet_diagnostic.CA1868.severity = warning

# CA2000: Dispose objects before losing scope
# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000
dotnet_diagnostic.CA2000.severity = none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,8 @@ internal IEnumerable<PSObject> QuerySession(
{
if (this.curCimSessionsById.ContainsKey(id))
{
if (!sessionIds.Contains(id))
if (sessionIds.Add(id))
{
sessionIds.Add(id);
sessions.Add(this.curCimSessionsById[id].GetPSObject());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ private static List<string> GetApplicableAccessRights(int accessMask, AccessRigh
foreach (string memberName in Enum.GetNames(accessRightType))
{
int memberValue = (int)Enum.Parse(accessRightType, memberName);
if (!foundAccessRightValues.Contains(memberValue))
if (foundAccessRightValues.Add(memberValue))
{
foundAccessRightValues.Add(memberValue);
if ((accessMask & memberValue) == memberValue)
{
foundAccessRightNames.Add(memberName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1419,11 +1419,7 @@ private static void ValidatePropertyNames(IList<string> names)
{
if (!string.IsNullOrEmpty(currentHeader))
{
if (!headers.Contains(currentHeader))
{
headers.Add(currentHeader);
}
else
if (!headers.Add(currentHeader))
{
// throw a terminating error as there are duplicate headers in the input.
string memberAlreadyPresentMsg =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,8 @@ private void ProcessTypeFiles()

if (ShouldProcess(formattedTarget, action))
{
if (!fullFileNameHash.Contains(resolvedPath))
if (fullFileNameHash.Add(resolvedPath))
{
fullFileNameHash.Add(resolvedPath);
newTypes.Add(new SessionStateTypeEntry(prependPathTotal[i]));
}
}
Expand All @@ -806,9 +805,8 @@ private void ProcessTypeFiles()
if (entry.FileName != null)
{
string resolvedPath = ModuleCmdletBase.ResolveRootedFilePath(entry.FileName, Context) ?? entry.FileName;
if (!fullFileNameHash.Contains(resolvedPath))
if (fullFileNameHash.Add(resolvedPath))
{
fullFileNameHash.Add(resolvedPath);
newTypes.Add(entry);
}
}
Expand All @@ -825,9 +823,8 @@ private void ProcessTypeFiles()

if (ShouldProcess(formattedTarget, action))
{
if (!fullFileNameHash.Contains(resolvedPath))
if (fullFileNameHash.Add(resolvedPath))
{
fullFileNameHash.Add(resolvedPath);
newTypes.Add(new SessionStateTypeEntry(appendPathTotalItem));
}
}
Expand Down Expand Up @@ -971,9 +968,8 @@ protected override void ProcessRecord()

if (ShouldProcess(formattedTarget, action))
{
if (!fullFileNameHash.Contains(appendPathTotalItem))
if (fullFileNameHash.Add(appendPathTotalItem))
{
fullFileNameHash.Add(appendPathTotalItem);
newFormats.Add(new SessionStateFormatEntry(appendPathTotalItem));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,7 @@ internal static AppliesTo GetAllApplicableTypes(TypeInfoDataBase db, AppliesTo a
// if it is a type reference, just add the type name
if (r is TypeReference tr)
{
if (!allTypes.Contains(tr.name))
allTypes.Add(tr.name);
allTypes.Add(tr.name);
}
else
{
Expand All @@ -619,8 +618,7 @@ internal static AppliesTo GetAllApplicableTypes(TypeInfoDataBase db, AppliesTo a
// we found the group, go over it
foreach (TypeReference x in tgd.typeReferenceList)
{
if (!allTypes.Contains(x.name))
allTypes.Add(x.name);
allTypes.Add(x.name);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/System.Management.Automation/engine/CommandDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,10 +1151,8 @@ internal void RegisterLookupCommandInfoAction(string currentAction, string comma
case "ActivePostCommand": currentActionSet = _activePostCommand; break;
}

if (currentActionSet.Contains(command))
if (!currentActionSet.Add(command))
throw new InvalidOperationException();
else
currentActionSet.Add(command);
}

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

if (currentActionSet.Contains(command))
currentActionSet.Remove(command);
currentActionSet.Remove(command);
}

private readonly HashSet<string> _activePreLookup = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1569,14 +1569,10 @@ public static InitialSessionState CreateDefault()
string assembly = ss.Assemblies[i].FileName;
if (!string.IsNullOrEmpty(assembly))
{
if (assemblyList.Contains(assembly))
if (!assemblyList.Add(assembly))
{
ss.Assemblies.RemoveItem(i);
}
else
{
assemblyList.Add(assembly);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,8 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(List<string> modulePat
PSModuleInfo module = CreateModuleInfoForGetModule(resolvedModulePath, refresh);
if (module != null)
{
if (!modules.Contains(resolvedModulePath))
if (modules.Add(resolvedModulePath))
{
modules.Add(resolvedModulePath);
yield return module;
}
}
Expand Down Expand Up @@ -1078,9 +1077,8 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(List<string> modulePat
foundModule = true;
// We need to list all versions of the module.
string subModulePath = Path.GetDirectoryName(file);
if (!modules.Contains(subModulePath))
if (modules.Add(subModulePath))
{
modules.Add(subModulePath);
yield return module;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/System.Management.Automation/engine/parser/PSType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,9 +1321,8 @@ internal static Assembly DefineTypes(Parser parser, Ast rootAst, TypeDefinitionA
foreach (var typeDefinitionAst in typeDefinitions)
{
var typeName = GetClassNameInAssembly(typeDefinitionAst);
if (!definedTypes.Contains(typeName))
if (definedTypes.Add(typeName))
{
definedTypes.Add(typeName);
if ((typeDefinitionAst.TypeAttributes & TypeAttributes.Class) == TypeAttributes.Class)
{
defineTypeHelpers.Add(new DefineTypeHelper(parser, module, typeDefinitionAst, typeName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,13 @@ private void CheckForDuplicateParameters(ReadOnlyCollection<ParameterAst> parame
foreach (var parameter in parameters)
{
string parameterName = parameter.Name.VariablePath.UserPath;
if (parametersSet.Contains(parameterName))
if (!parametersSet.Add(parameterName))
{
_parser.ReportError(parameter.Name.Extent,
nameof(ParserStrings.DuplicateFormalParameter),
ParserStrings.DuplicateFormalParameter,
parameterName);
}
else
{
parametersSet.Add(parameterName);
}

var voidConstraint =
parameter.Attributes.OfType<TypeConstraintAst>().FirstOrDefault(static t => typeof(void) == t.TypeName.GetReflectionType());
Expand Down Expand Up @@ -242,7 +238,7 @@ public override AstVisitAction VisitAttribute(AttributeAst attributeAst)
foreach (var namedArg in attributeAst.NamedArguments)
{
string name = namedArg.ArgumentName;
if (names.Contains(name))
if (!names.Add(name))
{
_parser.ReportError(namedArg.Extent,
nameof(ParserStrings.DuplicateNamedArgument),
Expand All @@ -251,8 +247,6 @@ public override AstVisitAction VisitAttribute(AttributeAst attributeAst)
}
else
{
names.Add(name);

if (!namedArg.ExpressionOmitted && !IsValidAttributeArgument(namedArg.Argument, constantValueVisitor))
{
var error = GetNonConstantAttributeArgErrorExpr(constantValueVisitor);
Expand Down Expand Up @@ -1124,7 +1118,7 @@ public override AstVisitAction VisitHashtable(HashtableAst hashtableAst)
if (keyStrAst != null)
{
var keyStr = keyStrAst.Value.ToString();
if (keys.Contains(keyStr))
if (!keys.Add(keyStr))
{
string errorId;
string errorMsg;
Expand All @@ -1141,10 +1135,6 @@ public override AstVisitAction VisitHashtable(HashtableAst hashtableAst)

_parser.ReportError(entry.Item1.Extent, errorId, errorMsg, keyStr);
}
else
{
keys.Add(keyStr);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,7 @@ private void HandleReadyForDisconnect(object sender, EventArgs args)
return;
}

if (_preparingForDisconnectList.Contains(bcmdTM))
{
_preparingForDisconnectList.Remove(bcmdTM);
}
_preparingForDisconnectList.Remove(bcmdTM);

if (_preparingForDisconnectList.Count == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,10 +1081,7 @@ internal override IEnumerable<HelpInfo> SearchHelp(HelpRequest helpRequest, bool
{
// this command is not visible to the user (from CommandOrigin) so
// dont show help topic for it.
if (!hiddenCommands.Contains(helpName))
{
hiddenCommands.Add(helpName);
}
hiddenCommands.Add(helpName);

continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ private Collection<string> FilterToLatestModuleVersion(Collection<string> filesM
{
string fileName = Path.GetFileName(file);

if (!fileNameHash.Contains(fileName))
{
fileNameHash.Add(fileName);
}
else
if (!fileNameHash.Add(fileName))
{
// If the file need to be removed, add it to matchedFilesToRemove, if not already present.
if (!matchedFilesToRemove.Contains(file))
Expand Down
3 changes: 1 addition & 2 deletions src/System.Management.Automation/security/CatalogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,8 @@ internal static void ProcessFileToBeAddedInCatalogDefinitionFile(FileInfo fileTo
relativePath = fileToHash.Name;
}

if (!relativePaths.Contains(relativePath))
if (relativePaths.Add(relativePath))
{
relativePaths.Add(relativePath);
if (fileToHash.Length != 0)
{
cdfFilesContent += "<HASH>" + fileToHash.FullName + "=" + fileToHash.FullName + Environment.NewLine;
Expand Down