Skip to content
Merged
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
36 changes: 11 additions & 25 deletions src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,22 +1432,13 @@ internal static void RemoveNestedModuleFunctions(PSModuleInfo module)
if ((input == null) || (input.Count == 0))
{ return; }

List<FunctionInfo> output = new List<FunctionInfo>(input.Count);
foreach (var fnInfo in input)
{
if (module.Name.Equals(fnInfo.ModuleName, StringComparison.OrdinalIgnoreCase))
{
output.Add(fnInfo);
}
}

input.Clear();
input.AddRange(output);
input.RemoveAll(fnInfo => !module.Name.Equals(fnInfo.ModuleName, StringComparison.OrdinalIgnoreCase));
}

#nullable enable
private static void SortAndRemoveDuplicates<T>(List<T> input, Func<T, string> keyGetter)
{
Dbg.Assert(input != null, "Caller should verify that input != null");
Dbg.Assert(input is not null, "Caller should verify that input != null");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove - we said about null items in the List.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I would update the code style while touching the method.


input.Sort(
(T x, T y) =>
Expand All @@ -1458,24 +1449,19 @@ private static void SortAndRemoveDuplicates<T>(List<T> input, Func<T, string> ke
}
);

bool firstItem = true;
string previousKey = null;
List<T> output = new List<T>(input.Count);
foreach (T item in input)
string? previousKey = null;
input.RemoveAll(ShouldRemove);

bool ShouldRemove(T item)
{
string currentKey = keyGetter(item);
if ((firstItem) || !currentKey.Equals(previousKey, StringComparison.OrdinalIgnoreCase))
{
output.Add(item);
}

bool match = previousKey is not null
&& currentKey.Equals(previousKey, StringComparison.OrdinalIgnoreCase);
previousKey = currentKey;
firstItem = false;
return match;
}

input.Clear();
input.AddRange(output);
}
#nullable restore

/// <summary>
/// Mark stuff to be exported from the current environment using the various patterns.
Expand Down