From e7a017589fb43d8b45981fe916fa4afffecc9390 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 20:34:48 +0000 Subject: [PATCH 01/12] Remove unecessary parenthesis --- src/System.Management.Automation/engine/GetCommandCommand.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 17efeb4ac73..08e826c2219 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1492,12 +1492,12 @@ private bool IsCommandInResult(CommandInfo command) bool commandHasModule = command.Module != null; foreach (CommandInfo commandInfo in _accumulatedResults) { - if ((command.CommandType == commandInfo.CommandType && + if (command.CommandType == commandInfo.CommandType && (string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) || // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. // Hence, an additional check is done with the prefix information string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase)) - ) && commandInfo.Module != null && commandHasModule && + && commandInfo.Module != null && commandHasModule && ( // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path (commandInfo.IsImported && command.IsImported && commandInfo.Module.Equals(command.Module)) || ((!commandInfo.IsImported || !command.IsImported) && commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase)) From a6ec51e64229ca15da93842c3856bda79e4fcfeb Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 20:35:06 +0000 Subject: [PATCH 02/12] Invert if --- .../engine/GetCommandCommand.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 08e826c2219..4d7d35f480e 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1492,20 +1492,22 @@ private bool IsCommandInResult(CommandInfo command) bool commandHasModule = command.Module != null; foreach (CommandInfo commandInfo in _accumulatedResults) { - if (command.CommandType == commandInfo.CommandType && - (string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) || + if (command.CommandType != commandInfo.CommandType || + (!string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) && // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. // Hence, an additional check is done with the prefix information - string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase)) - && commandInfo.Module != null && commandHasModule && + !string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase)) + || commandInfo.Module == null || !commandHasModule || ( // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path - (commandInfo.IsImported && command.IsImported && commandInfo.Module.Equals(command.Module)) || - ((!commandInfo.IsImported || !command.IsImported) && commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase)) + (!commandInfo.IsImported || !command.IsImported || !commandInfo.Module.Equals(command.Module)) && + ((commandInfo.IsImported && command.IsImported) || !commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase)) )) { - isPresent = true; - break; + continue; } + + isPresent = true; + break; } return isPresent; From dc74a353fcd7536bddfcde2bfb6c14ece1b23c9b Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 20:38:19 +0000 Subject: [PATCH 03/12] Split into consecutive if statements --- .../engine/GetCommandCommand.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 4d7d35f480e..145bae3ac69 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1492,13 +1492,25 @@ private bool IsCommandInResult(CommandInfo command) bool commandHasModule = command.Module != null; foreach (CommandInfo commandInfo in _accumulatedResults) { - if (command.CommandType != commandInfo.CommandType || - (!string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) && + if (command.CommandType != commandInfo.CommandType) + { + continue; + } + + if ((!string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) && // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. // Hence, an additional check is done with the prefix information - !string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase)) - || commandInfo.Module == null || !commandHasModule || - ( // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path + !string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase))) + { + continue; + } + + if (commandInfo.Module == null || !commandHasModule) + { + continue; + } + + if (( // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path (!commandInfo.IsImported || !command.IsImported || !commandInfo.Module.Equals(command.Module)) && ((commandInfo.IsImported && command.IsImported) || !commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase)) )) From c2f539cf348b631e2c6af7d8ea09654fb313180f Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 20:45:52 +0000 Subject: [PATCH 04/12] Inline temporary variable --- src/System.Management.Automation/engine/GetCommandCommand.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 145bae3ac69..8f03fa679df 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1489,7 +1489,6 @@ private IEnumerable GetMatchingCommandsFromModules(string commandNa private bool IsCommandInResult(CommandInfo command) { bool isPresent = false; - bool commandHasModule = command.Module != null; foreach (CommandInfo commandInfo in _accumulatedResults) { if (command.CommandType != commandInfo.CommandType) @@ -1505,7 +1504,7 @@ private bool IsCommandInResult(CommandInfo command) continue; } - if (commandInfo.Module == null || !commandHasModule) + if (commandInfo.Module == null || !(command.Module != null)) { continue; } From 921ad4afca24dd3fe4c5e4d82e44fbbc03e9c74d Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 20:48:00 +0000 Subject: [PATCH 05/12] Split into consecutive if statements --- .../engine/GetCommandCommand.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 8f03fa679df..7d73a0d4350 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1504,7 +1504,12 @@ private bool IsCommandInResult(CommandInfo command) continue; } - if (commandInfo.Module == null || !(command.Module != null)) + if (commandInfo.Module == null) + { + continue; + } + + if (!(command.Module != null)) { continue; } From efddec4a0638d507b3a99a2925e9f718f41d8445 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 20:58:21 +0000 Subject: [PATCH 06/12] Reorder if statements --- .../engine/GetCommandCommand.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 7d73a0d4350..ee47d64d9da 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1491,15 +1491,7 @@ private bool IsCommandInResult(CommandInfo command) bool isPresent = false; foreach (CommandInfo commandInfo in _accumulatedResults) { - if (command.CommandType != commandInfo.CommandType) - { - continue; - } - - if ((!string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) && - // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. - // Hence, an additional check is done with the prefix information - !string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase))) + if (!(command.Module != null)) { continue; } @@ -1509,7 +1501,7 @@ private bool IsCommandInResult(CommandInfo command) continue; } - if (!(command.Module != null)) + if (command.CommandType != commandInfo.CommandType) { continue; } @@ -1522,6 +1514,14 @@ private bool IsCommandInResult(CommandInfo command) continue; } + if ((!string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) && + // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. + // Hence, an additional check is done with the prefix information + !string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase))) + { + continue; + } + isPresent = true; break; } From 7313e4e5af68dc5f6ec584a48917a43993603ab7 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 21:00:44 +0000 Subject: [PATCH 07/12] Hoist loop-invariant code --- .../engine/GetCommandCommand.cs | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index ee47d64d9da..391281359ac 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1489,41 +1489,40 @@ private IEnumerable GetMatchingCommandsFromModules(string commandNa private bool IsCommandInResult(CommandInfo command) { bool isPresent = false; - foreach (CommandInfo commandInfo in _accumulatedResults) + + if (command.Module != null) { - if (!(command.Module != null)) + foreach (CommandInfo commandInfo in _accumulatedResults) { - continue; - } + if (commandInfo.Module == null) + { + continue; + } - if (commandInfo.Module == null) - { - continue; - } + if (command.CommandType != commandInfo.CommandType) + { + continue; + } - if (command.CommandType != commandInfo.CommandType) - { - continue; - } + if (( // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path + (!commandInfo.IsImported || !command.IsImported || !commandInfo.Module.Equals(command.Module)) && + ((commandInfo.IsImported && command.IsImported) || !commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase)) + )) + { + continue; + } - if (( // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path - (!commandInfo.IsImported || !command.IsImported || !commandInfo.Module.Equals(command.Module)) && - ((commandInfo.IsImported && command.IsImported) || !commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase)) - )) - { - continue; - } + if ((!string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) && + // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. + // Hence, an additional check is done with the prefix information + !string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase))) + { + continue; + } - if ((!string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) && - // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. - // Hence, an additional check is done with the prefix information - !string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase))) - { - continue; + isPresent = true; + break; } - - isPresent = true; - break; } return isPresent; From df55e4d35edc6a88b7c5b3605b135a1ed4be2ac7 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 21:04:30 +0000 Subject: [PATCH 08/12] Move iteration variable to left-hand side of expressions --- .../engine/GetCommandCommand.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 391281359ac..e49afaa5f3b 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1499,7 +1499,7 @@ private bool IsCommandInResult(CommandInfo command) continue; } - if (command.CommandType != commandInfo.CommandType) + if (commandInfo.CommandType != command.CommandType) { continue; } @@ -1512,10 +1512,10 @@ private bool IsCommandInResult(CommandInfo command) continue; } - if ((!string.Equals(command.Name, commandInfo.Name, StringComparison.OrdinalIgnoreCase) && + if ((!commandInfo.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase) && // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. // Hence, an additional check is done with the prefix information - !string.Equals(ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix), command.Name, StringComparison.OrdinalIgnoreCase))) + !ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix).Equals(command.Name, StringComparison.OrdinalIgnoreCase))) { continue; } From 86439691f7edf8555fcf32589c08ec5e83b61cfb Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 21:05:20 +0000 Subject: [PATCH 09/12] Merge with previous if statement --- .../engine/GetCommandCommand.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index e49afaa5f3b..22cf6304028 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1494,12 +1494,7 @@ private bool IsCommandInResult(CommandInfo command) { foreach (CommandInfo commandInfo in _accumulatedResults) { - if (commandInfo.Module == null) - { - continue; - } - - if (commandInfo.CommandType != command.CommandType) + if (commandInfo.Module == null || commandInfo.CommandType != command.CommandType) { continue; } From a495496b68535c70479cff36a2bfa393415639d6 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 21:07:09 +0000 Subject: [PATCH 10/12] Reformat --- .../engine/GetCommandCommand.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 22cf6304028..1283b24f53a 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1499,18 +1499,17 @@ private bool IsCommandInResult(CommandInfo command) continue; } - if (( // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path - (!commandInfo.IsImported || !command.IsImported || !commandInfo.Module.Equals(command.Module)) && - ((commandInfo.IsImported && command.IsImported) || !commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase)) - )) + // We do reference equal comparison if both command are imported. If either one is not imported, we compare the module path + if ((!commandInfo.IsImported || !command.IsImported || !commandInfo.Module.Equals(command.Module)) + && ((commandInfo.IsImported && command.IsImported) || !commandInfo.Module.Path.Equals(command.Module.Path, StringComparison.OrdinalIgnoreCase))) { continue; } - if ((!commandInfo.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase) && - // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. - // Hence, an additional check is done with the prefix information - !ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix).Equals(command.Name, StringComparison.OrdinalIgnoreCase))) + // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. + // Hence, an additional check is done with the prefix information + if (!commandInfo.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase) + && !ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix).Equals(command.Name, StringComparison.OrdinalIgnoreCase)) { continue; } From 27191f4c57a15407f995b209eba4b2913aa4f6ad Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 21:07:49 +0000 Subject: [PATCH 11/12] Invert if --- .../engine/GetCommandCommand.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 1283b24f53a..bcd9e5556f0 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1508,14 +1508,12 @@ private bool IsCommandInResult(CommandInfo command) // If the command has been imported with a prefix, then just checking the names for duplication will not be enough. // Hence, an additional check is done with the prefix information - if (!commandInfo.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase) - && !ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix).Equals(command.Name, StringComparison.OrdinalIgnoreCase)) + if (commandInfo.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase) + || ModuleCmdletBase.RemovePrefixFromCommandName(commandInfo.Name, commandInfo.Prefix).Equals(command.Name, StringComparison.OrdinalIgnoreCase)) { - continue; + isPresent = true; + break; } - - isPresent = true; - break; } } From 89fd067eab1b9f2e88088f6c76ac6828ae66ae26 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 11 Dec 2020 21:09:15 +0000 Subject: [PATCH 12/12] Use pattern matching --- src/System.Management.Automation/engine/GetCommandCommand.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index bcd9e5556f0..a6c15f1fbc0 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1490,11 +1490,11 @@ private bool IsCommandInResult(CommandInfo command) { bool isPresent = false; - if (command.Module != null) + if (command.Module is not null) { foreach (CommandInfo commandInfo in _accumulatedResults) { - if (commandInfo.Module == null || commandInfo.CommandType != command.CommandType) + if (commandInfo.Module is null || commandInfo.CommandType != command.CommandType) { continue; }