From 5c9d7086b1e56d866746bf5649a965d32d012144 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 15:55:05 +0100 Subject: [PATCH 01/27] changed 10 files --- .../ManagementList/Common/WpfHelp.cs | 5 +---- .../PropertyValueSelectorFilterRule.cs | 10 ++------- .../FilterCore/ValidatingValueBase.cs | 5 +---- .../FilterProviders/FilterRulePanelItem.cs | 5 +---- .../ManagementList/DefaultStringConverter.cs | 4 +++- .../ViewModel/AllModulesViewModel.cs | 6 ++++-- .../cimSupport/cmdletization/cim/cimQuery.cs | 5 +---- .../Common/InvokeRestMethodCommand.Common.cs | 2 +- .../utils/MshTraceSource.cs | 21 ++++++------------- .../perfCounters/CounterSetRegistrarBase.cs | 7 +++++-- 10 files changed, 25 insertions(+), 45 deletions(-) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs index b0839c6ccd2..c03b7b1bb02 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs @@ -425,10 +425,7 @@ public static T GetOptionalTemplateChild(Control templateParent, string child { ArgumentNullException.ThrowIfNull(templateParent); - if (string.IsNullOrEmpty(childName)) - { - throw new ArgumentNullException("childName"); - } + ArgumentException.ThrowIfNullOrEmpty(childName); object templatePart = templateParent.Template.FindName(childName, templateParent); T item = templatePart as T; diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/FilterRules/PropertyValueSelectorFilterRule.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/FilterRules/PropertyValueSelectorFilterRule.cs index f1bf8520a99..2405273c6a7 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/FilterRules/PropertyValueSelectorFilterRule.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/FilterRules/PropertyValueSelectorFilterRule.cs @@ -66,15 +66,9 @@ public PropertyValueSelectorFilterRule(string propertyName, string propertyDispl /// public PropertyValueSelectorFilterRule(string propertyName, string propertyDisplayName, IEnumerable rules) { - if (string.IsNullOrEmpty(propertyName)) - { - throw new ArgumentNullException("propertyName"); - } + ArgumentException.ThrowIfNullOrEmpty(propertyName); - if (string.IsNullOrEmpty(propertyDisplayName)) - { - throw new ArgumentNullException("propertyDisplayName"); - } + ArgumentException.ThrowIfNullOrEmpty(propertyDisplayName); ArgumentNullException.ThrowIfNull(rules); diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs index f33862846a9..ab63bce9f27 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs @@ -83,10 +83,7 @@ public string this[string columnName] { get { - if (string.IsNullOrEmpty(columnName)) - { - throw new ArgumentNullException("columnName"); - } + ArgumentException.ThrowIfNullOrEmpty(columnName); this.UpdateValidationResult(columnName); return this.GetValidationResult().ErrorMessage; diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRulePanelItem.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRulePanelItem.cs index 2378f9238d8..0887087f505 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRulePanelItem.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRulePanelItem.cs @@ -81,10 +81,7 @@ public FilterRulePanelItem(FilterRule rule, string groupId) { ArgumentNullException.ThrowIfNull(rule); - if (string.IsNullOrEmpty(groupId)) - { - throw new ArgumentNullException("groupId"); - } + ArgumentException.ThrowIfNullOrEmpty(groupId); this.Rule = rule; this.GroupId = groupId; diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs index cf85d79ae36..2d590904097 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs @@ -62,7 +62,9 @@ public string DefaultValue /// public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { - if (values == null || values.Length != 1) + ArgumentNullException.ThrowIfNull(values); + + if (values.Length != 1) { throw new ArgumentNullException("values"); } diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs index da51550c084..97884db7909 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs @@ -79,11 +79,13 @@ public class AllModulesViewModel : INotifyPropertyChanged /// Commands to show. public AllModulesViewModel(Dictionary importedModules, IEnumerable commands) { - if (commands == null || !commands.GetEnumerator().MoveNext()) + ArgumentNullException.ThrowIfNull(commands); + + if (!commands.GetEnumerator().MoveNext()) { throw new ArgumentNullException("commands"); } - + this.Initialization(importedModules, commands, true); } diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimQuery.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimQuery.cs index 2c5c91fb1c8..4c2a6521772 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimQuery.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimQuery.cs @@ -314,10 +314,7 @@ public override void FilterByAssociatedInstance(object associatedInstance, strin /// public override void AddQueryOption(string optionName, object optionValue) { - if (string.IsNullOrEmpty(optionName)) - { - throw new ArgumentNullException(nameof(optionName)); - } + ArgumentException.ThrowIfNullOrEmpty(optionName); ArgumentNullException.ThrowIfNull(optionValue); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 50a88bb6754..35abfecab26 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -189,7 +189,7 @@ internal override void ProcessResponse(HttpResponseMessage response) private static RestReturnType CheckReturnType(HttpResponseMessage response) { - if (response == null) { throw new ArgumentNullException(nameof(response)); } + ArgumentException.ThrowIfNull(response); RestReturnType rt = RestReturnType.Detect; string contentType = ContentHelper.GetContentType(response); diff --git a/src/System.Management.Automation/utils/MshTraceSource.cs b/src/System.Management.Automation/utils/MshTraceSource.cs index 25fcc7b940e..3eaa168d393 100644 --- a/src/System.Management.Automation/utils/MshTraceSource.cs +++ b/src/System.Management.Automation/utils/MshTraceSource.cs @@ -98,13 +98,10 @@ internal static PSTraceSource GetTracer( string description, bool traceHeaders) { - if (string.IsNullOrEmpty(name)) - { - // 2005/04/13-JonN In theory this should be ArgumentException, - // but I don't want to deal with loading the string in this - // low-level code. - throw new ArgumentNullException(nameof(name)); - } + // 2005/04/13-JonN In theory this should be ArgumentException, + // but I don't want to deal with loading the string in this + // low-level code. + ArgumentException.ThrowIfNullOrEmpty(name); lock (PSTraceSource.s_getTracerLock) { @@ -228,10 +225,7 @@ internal static PSTraceSource GetNewTraceSource( /// Exception instance ready to throw. internal static PSArgumentNullException NewArgumentNullException(string paramName) { - if (string.IsNullOrEmpty(paramName)) - { - throw new ArgumentNullException(nameof(paramName)); - } + ArgumentException.ThrowIfNullOrEmpty(paramName); string message = StringUtil.Format(AutomationExceptions.ArgumentNull, paramName); var e = new PSArgumentNullException(paramName, message); @@ -287,10 +281,7 @@ internal static PSArgumentNullException NewArgumentNullException( /// Exception instance ready to throw. internal static PSArgumentException NewArgumentException(string paramName) { - if (string.IsNullOrEmpty(paramName)) - { - throw new ArgumentNullException(nameof(paramName)); - } + ArgumentException.ThrowIfNullOrEmpty(paramName); string message = StringUtil.Format(AutomationExceptions.Argument, paramName); // Note that the message param comes first diff --git a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs index 917e04a5e69..623eb74849e 100644 --- a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs +++ b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs @@ -107,11 +107,14 @@ protected CounterSetRegistrarBase( CounterSetId = counterSetId; CounterSetInstType = counterSetInstType; CounterSetName = counterSetName; - if ((counterInfoArray == null) - || (counterInfoArray.Length == 0)) + + ArgumentException.ThrowIfNull(counterInfoArray); + + if (counterInfoArray.Length == 0) { throw new ArgumentNullException(nameof(counterInfoArray)); } + CounterInfoArray = new CounterInfo[counterInfoArray.Length]; From 0dc86e75e01fa29d8fb124409b2ef5373006245d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 16:05:18 +0100 Subject: [PATCH 02/27] changed 11 files --- src/Microsoft.WSMan.Management/ConfigProvider.cs | 4 +++- .../CurrentConfigurations.cs | 5 +---- .../engine/InitialSessionState.cs | 6 +----- .../engine/Modules/ModuleSpecification.cs | 5 +---- .../engine/Modules/PSModuleInfo.cs | 5 +---- .../engine/PSClassInfo.cs | 3 +-- .../engine/lang/scriptblock.cs | 4 +++- .../engine/remoting/commands/InvokeCommandCommand.cs | 5 +---- .../utils/MshTraceSource.cs | 5 +---- .../utils/ParserException.cs | 4 +++- .../utils/StructuredTraceSource.cs | 11 ++++------- 11 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs index de99812b01d..d91acdedb91 100644 --- a/src/Microsoft.WSMan.Management/ConfigProvider.cs +++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs @@ -3413,7 +3413,9 @@ private static string NormalizePath(string path, string host) /// private PSObject GetItemValue(string path) { - if (string.IsNullOrEmpty(path) || (path.Length == 0)) + ArgumentException.ThrowIfNullOrEmpty(path ); + + if (path.Length == 0) { throw new ArgumentNullException(path); } diff --git a/src/Microsoft.WSMan.Management/CurrentConfigurations.cs b/src/Microsoft.WSMan.Management/CurrentConfigurations.cs index 81d4ecfd870..6bfbbe8d78d 100644 --- a/src/Microsoft.WSMan.Management/CurrentConfigurations.cs +++ b/src/Microsoft.WSMan.Management/CurrentConfigurations.cs @@ -76,10 +76,7 @@ public CurrentConfigurations(IWSManSession serverSession) /// False, if operation failed. public bool RefreshCurrentConfiguration(string responseOfGet) { - if (string.IsNullOrEmpty(responseOfGet)) - { - throw new ArgumentNullException(nameof(responseOfGet)); - } + ArgumentException.ThrowIfNullOrEmpty(responseOfGet); this.rootDocument.LoadXml(responseOfGet); this.documentElement = this.rootDocument.DocumentElement; diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 2dbb12fb253..c2c06d8ffce 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -3738,11 +3738,7 @@ internal PSSnapInInfo ImportCorePSSnapIn() internal PSSnapInInfo ImportPSSnapIn(PSSnapInInfo psSnapInInfo, out PSSnapInException warning) { - if (psSnapInInfo == null) - { - ArgumentNullException e = new ArgumentNullException(nameof(psSnapInInfo)); - throw e; - } + ArgumentException.ThrowIfNull(psSnapInInfo); // See if the snapin is already loaded. If has been then there will be an entry in the // Assemblies list for it already... diff --git a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs index b539b191909..cdabb8b7d1e 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs @@ -44,10 +44,7 @@ public ModuleSpecification() /// The module name. public ModuleSpecification(string moduleName) { - if (string.IsNullOrEmpty(moduleName)) - { - throw new ArgumentNullException(nameof(moduleName)); - } + ArgumentException.ThrowIfNullOrEmpty(moduleName); this.Name = moduleName; // Alias name of miniumVersion diff --git a/src/System.Management.Automation/engine/Modules/PSModuleInfo.cs b/src/System.Management.Automation/engine/Modules/PSModuleInfo.cs index 8dae7db0deb..6dddb43a0dd 100644 --- a/src/System.Management.Automation/engine/Modules/PSModuleInfo.cs +++ b/src/System.Management.Automation/engine/Modules/PSModuleInfo.cs @@ -1302,10 +1302,7 @@ public object Invoke(ScriptBlock sb, params object[] args) /// public PSVariable GetVariableFromCallersModule(string variableName) { - if (string.IsNullOrEmpty(variableName)) - { - throw new ArgumentNullException(nameof(variableName)); - } + ArgumentException.ThrowIfNullOrEmpty(variableName); var context = LocalPipeline.GetExecutionContextFromTLS(); SessionState callersSessionState = null; diff --git a/src/System.Management.Automation/engine/PSClassInfo.cs b/src/System.Management.Automation/engine/PSClassInfo.cs index fd0ca8d8936..10b2cf1101d 100644 --- a/src/System.Management.Automation/engine/PSClassInfo.cs +++ b/src/System.Management.Automation/engine/PSClassInfo.cs @@ -61,8 +61,7 @@ public sealed class PSClassMemberInfo /// internal PSClassMemberInfo(string name, string memberType, string defaultValue) { - if (string.IsNullOrEmpty(name)) - throw new ArgumentNullException(nameof(name)); + ArgumentException.ThrowIfNullOrEmpty(name); this.Name = name; this.TypeName = memberType; diff --git a/src/System.Management.Automation/engine/lang/scriptblock.cs b/src/System.Management.Automation/engine/lang/scriptblock.cs index 3c7d6576aaa..3ad3e9d3f18 100644 --- a/src/System.Management.Automation/engine/lang/scriptblock.cs +++ b/src/System.Management.Automation/engine/lang/scriptblock.cs @@ -1138,7 +1138,9 @@ public void Begin(bool expectInput, EngineIntrinsics contextToRedirectTo) /// The command you're calling this from (i.e. instance of PSCmdlet or value of $PSCmdlet variable). public void Begin(InternalCommand command) { - if (command == null || command.MyInvocation == null) + ArgumentException.ThrowIfNull(command); + + if (command.MyInvocation == null) { throw new ArgumentNullException(nameof(command)); } diff --git a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs index a2a2397cb7a..d56af99f7c7 100644 --- a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs @@ -2167,10 +2167,7 @@ public void StartProgress( return; } - if (string.IsNullOrEmpty(computerName)) - { - throw new ArgumentNullException(nameof(computerName)); - } + ArgumentException.ThrowIfNullOrEmpty(computerName); lock (_syncObject) { diff --git a/src/System.Management.Automation/utils/MshTraceSource.cs b/src/System.Management.Automation/utils/MshTraceSource.cs index 3eaa168d393..7a6029d84b9 100644 --- a/src/System.Management.Automation/utils/MshTraceSource.cs +++ b/src/System.Management.Automation/utils/MshTraceSource.cs @@ -468,10 +468,7 @@ internal static PSNotImplementedException NewNotImplementedException() /// Exception instance ready to throw. internal static PSArgumentOutOfRangeException NewArgumentOutOfRangeException(string paramName, object actualValue) { - if (string.IsNullOrEmpty(paramName)) - { - throw new ArgumentNullException(nameof(paramName)); - } + ArgumentException.ThrowIfNullOrEmpty(paramName); string message = StringUtil.Format(AutomationExceptions.ArgumentOutOfRange, paramName); var e = new PSArgumentOutOfRangeException(paramName, actualValue, message); diff --git a/src/System.Management.Automation/utils/ParserException.cs b/src/System.Management.Automation/utils/ParserException.cs index 4c953be0276..ebfbf9769b2 100644 --- a/src/System.Management.Automation/utils/ParserException.cs +++ b/src/System.Management.Automation/utils/ParserException.cs @@ -131,7 +131,9 @@ public ParseException(string message, Justification = "ErrorRecord is not overridden in classes deriving from ParseException")] public ParseException(Language.ParseError[] errors) { - if ((errors == null) || (errors.Length == 0)) + ArgumentException.ThrowIfNull(errors); + + if (errors.Length == 0) { throw new ArgumentNullException(nameof(errors)); } diff --git a/src/System.Management.Automation/utils/StructuredTraceSource.cs b/src/System.Management.Automation/utils/StructuredTraceSource.cs index 8d47ac61c9d..c13e9e5fe3d 100644 --- a/src/System.Management.Automation/utils/StructuredTraceSource.cs +++ b/src/System.Management.Automation/utils/StructuredTraceSource.cs @@ -269,13 +269,10 @@ public partial class PSTraceSource /// internal PSTraceSource(string fullName, string name, string description, bool traceHeaders) { - if (string.IsNullOrEmpty(fullName)) - { - // 2005/04/13-JonN In theory this should be ArgumentException, - // but I don't want to deal with loading the string in this - // low-level code. - throw new ArgumentNullException(nameof(fullName)); - } + // 2005/04/13-JonN In theory this should be ArgumentException, + // but I don't want to deal with loading the string in this + // low-level code. + ArgumentException.ThrowIfNullOrEmpty(fullName); try { From 6eff253c6b29ac7e7f6b1c469dfe5fd7eb9178a6 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 16:15:28 +0100 Subject: [PATCH 03/27] changed 9 files --- .../ManagementList/Common/WpfHelp.cs | 5 +---- .../cimSupport/cmdletization/SessionBasedWrapper.cs | 2 +- .../commands/management/TestConnectionCommand.cs | 2 +- .../DotNetCode/Eventing/EventProviderTraceListener.cs | 3 +-- src/Microsoft.WSMan.Management/CurrentConfigurations.cs | 5 +---- src/Microsoft.WSMan.Management/WsManHelper.cs | 5 +---- .../CoreCLR/CorePsAssemblyLoadContext.cs | 3 +-- src/System.Management.Automation/utils/ParserException.cs | 5 +---- .../utils/perfCounters/CounterSetRegistrarBase.cs | 6 +----- 9 files changed, 9 insertions(+), 27 deletions(-) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs index c03b7b1bb02..8db2c626d6d 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs @@ -214,10 +214,7 @@ public static void AddChild(FrameworkElement parent, FrameworkElement element) { ArgumentNullException.ThrowIfNull(element); - if (parent == null) - { - throw new ArgumentNullException("element"); - } + ArgumentNullException.ThrowIfNull(parent, "element"); ContentControl parentContentControl = parent as ContentControl; diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs index 58a1fbb4c2d..f7154f8dffe 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs @@ -82,7 +82,7 @@ protected TSession[] Session set { - _session = value ?? throw new ArgumentNullException(nameof(value)); + _session = value ?? ArgumentException.ThrowIfNull(value); _sessionWasSpecified = true; } } diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e6b33142457..e9085d8b6df 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -578,7 +578,7 @@ private void ProcessMTUSize(string targetNameOrAddress) WriteObject(new PingMtuStatus( Source, resolvedTargetName, - replyResult ?? throw new ArgumentNullException(nameof(replyResult)), + replyResult ?? ArgumentException.ThrowIfNull(replyResult), CurrentMTUSize)); } } diff --git a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs index 94eb340b21c..1c82891b654 100644 --- a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs +++ b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs @@ -41,8 +41,7 @@ public string Delimiter [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly")] set { - if (value == null) - throw new ArgumentNullException(nameof(Delimiter)); + ArgumentNullException.ThrowIfNull(value, nameof(Delimiter)); if (value.Length == 0) throw new ArgumentException(DotNetEventingStrings.Argument_NeedNonemptyDelimiter); diff --git a/src/Microsoft.WSMan.Management/CurrentConfigurations.cs b/src/Microsoft.WSMan.Management/CurrentConfigurations.cs index 6bfbbe8d78d..641f558d432 100644 --- a/src/Microsoft.WSMan.Management/CurrentConfigurations.cs +++ b/src/Microsoft.WSMan.Management/CurrentConfigurations.cs @@ -95,10 +95,7 @@ public bool RefreshCurrentConfiguration(string responseOfGet) /// False, if operation is not successful. public void PutConfigurationOnServer(string resourceUri) { - if (string.IsNullOrEmpty(resourceUri)) - { - throw new ArgumentNullException(nameof(resourceUri)); - } + ArgumentException.ThrowIfNullOrEmpty(resourceUri); this.serverSession.Put(resourceUri, this.rootDocument.InnerXml, 0); } diff --git a/src/Microsoft.WSMan.Management/WsManHelper.cs b/src/Microsoft.WSMan.Management/WsManHelper.cs index 71672f3232f..0c3dfebe8f3 100644 --- a/src/Microsoft.WSMan.Management/WsManHelper.cs +++ b/src/Microsoft.WSMan.Management/WsManHelper.cs @@ -180,10 +180,7 @@ private static string FormatResourceMsgFromResourcetextS( { ArgumentNullException.ThrowIfNull(resourceManager); - if (string.IsNullOrEmpty(resourceName)) - { - throw new ArgumentNullException(nameof(resourceName)); - } + ArgumentException.ThrowIfNullOrEmpty(resourceName); string template = resourceManager.GetString(resourceName); diff --git a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs index 2e4e7fe0f9b..93b9dc2d6e0 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs @@ -579,8 +579,7 @@ public static class PowerShellAssemblyLoadContextInitializer /// public static void SetPowerShellAssemblyLoadContext([MarshalAs(UnmanagedType.LPWStr)] string basePaths) { - if (string.IsNullOrEmpty(basePaths)) - throw new ArgumentNullException(nameof(basePaths)); + ArgumentException.ThrowIfNullOrEmpty(basePaths); PowerShellAssemblyLoadContext.InitializeSingleton(basePaths); } diff --git a/src/System.Management.Automation/utils/ParserException.cs b/src/System.Management.Automation/utils/ParserException.cs index ebfbf9769b2..4cf9d0992b0 100644 --- a/src/System.Management.Automation/utils/ParserException.cs +++ b/src/System.Management.Automation/utils/ParserException.cs @@ -133,10 +133,7 @@ public ParseException(Language.ParseError[] errors) { ArgumentException.ThrowIfNull(errors); - if (errors.Length == 0) - { - throw new ArgumentNullException(nameof(errors)); - } + ArgumentOutOfRangeException.ThrowIfZero(errors.Length); _errors = errors; // Arbitrarily choose the first error message for the ErrorId. diff --git a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs index 623eb74849e..0b50730debf 100644 --- a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs +++ b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs @@ -110,12 +110,8 @@ protected CounterSetRegistrarBase( ArgumentException.ThrowIfNull(counterInfoArray); - if (counterInfoArray.Length == 0) - { - throw new ArgumentNullException(nameof(counterInfoArray)); - } + ArgumentOutOfRangeException.ThrowIfZero(counterInfoArray.Length); - CounterInfoArray = new CounterInfo[counterInfoArray.Length]; for (int i = 0; i < counterInfoArray.Length; i++) From 47c5609e39c0039ef01e6945bb67c09c5e24f3c6 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 16:21:38 +0100 Subject: [PATCH 04/27] fix copy-pasting error --- .../cimSupport/cmdletization/SessionBasedWrapper.cs | 2 +- .../commands/management/TestConnectionCommand.cs | 2 +- .../utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 2 +- src/System.Management.Automation/engine/InitialSessionState.cs | 2 +- src/System.Management.Automation/engine/lang/scriptblock.cs | 2 +- src/System.Management.Automation/utils/ParserException.cs | 2 +- .../utils/perfCounters/CounterSetRegistrarBase.cs | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs index f7154f8dffe..037fed9f92a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs @@ -82,7 +82,7 @@ protected TSession[] Session set { - _session = value ?? ArgumentException.ThrowIfNull(value); + _session = value ?? ArgumentNullException.ThrowIfNull(value); _sessionWasSpecified = true; } } diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e9085d8b6df..efc0c6dbec4 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -578,7 +578,7 @@ private void ProcessMTUSize(string targetNameOrAddress) WriteObject(new PingMtuStatus( Source, resolvedTargetName, - replyResult ?? ArgumentException.ThrowIfNull(replyResult), + replyResult ?? ArgumentNullException.ThrowIfNull(replyResult), CurrentMTUSize)); } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 35abfecab26..a6a2a6290b0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -189,7 +189,7 @@ internal override void ProcessResponse(HttpResponseMessage response) private static RestReturnType CheckReturnType(HttpResponseMessage response) { - ArgumentException.ThrowIfNull(response); + ArgumentNullException.ThrowIfNull(response); RestReturnType rt = RestReturnType.Detect; string contentType = ContentHelper.GetContentType(response); diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index c2c06d8ffce..670fe4a69cf 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -3738,7 +3738,7 @@ internal PSSnapInInfo ImportCorePSSnapIn() internal PSSnapInInfo ImportPSSnapIn(PSSnapInInfo psSnapInInfo, out PSSnapInException warning) { - ArgumentException.ThrowIfNull(psSnapInInfo); + ArgumentNullException.ThrowIfNull(psSnapInInfo); // See if the snapin is already loaded. If has been then there will be an entry in the // Assemblies list for it already... diff --git a/src/System.Management.Automation/engine/lang/scriptblock.cs b/src/System.Management.Automation/engine/lang/scriptblock.cs index 3ad3e9d3f18..738c411bffd 100644 --- a/src/System.Management.Automation/engine/lang/scriptblock.cs +++ b/src/System.Management.Automation/engine/lang/scriptblock.cs @@ -1138,7 +1138,7 @@ public void Begin(bool expectInput, EngineIntrinsics contextToRedirectTo) /// The command you're calling this from (i.e. instance of PSCmdlet or value of $PSCmdlet variable). public void Begin(InternalCommand command) { - ArgumentException.ThrowIfNull(command); + ArgumentNullException.ThrowIfNull(command); if (command.MyInvocation == null) { diff --git a/src/System.Management.Automation/utils/ParserException.cs b/src/System.Management.Automation/utils/ParserException.cs index 4cf9d0992b0..0fcda917c3b 100644 --- a/src/System.Management.Automation/utils/ParserException.cs +++ b/src/System.Management.Automation/utils/ParserException.cs @@ -131,7 +131,7 @@ public ParseException(string message, Justification = "ErrorRecord is not overridden in classes deriving from ParseException")] public ParseException(Language.ParseError[] errors) { - ArgumentException.ThrowIfNull(errors); + ArgumentNullException.ThrowIfNull(errors); ArgumentOutOfRangeException.ThrowIfZero(errors.Length); diff --git a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs index 0b50730debf..fc6361b0351 100644 --- a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs +++ b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs @@ -108,7 +108,7 @@ protected CounterSetRegistrarBase( CounterSetInstType = counterSetInstType; CounterSetName = counterSetName; - ArgumentException.ThrowIfNull(counterInfoArray); + ArgumentNullException.ThrowIfNull(counterInfoArray); ArgumentOutOfRangeException.ThrowIfZero(counterInfoArray.Length); From c9cbbd0a3c6211d75894076a6c3726142f60a8f3 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 16:31:45 +0100 Subject: [PATCH 05/27] changed 10 files --- .../Utils.cs | 5 +---- .../ManagementList/Common/WpfHelp.cs | 5 +---- .../ConfigProvider.cs | 5 +---- .../CurrentConfigurations.cs | 5 +---- .../cmdletization/ObjectModelWrapper.cs | 5 +---- .../engine/CommandInfo.cs | 5 +---- .../engine/GetCommandCommand.cs | 2 +- .../engine/InitialSessionState.cs | 6 +----- .../engine/Utils.cs | 17 +++++------------ .../namespaces/FileSystemProvider.cs | 10 ++-------- 10 files changed, 15 insertions(+), 50 deletions(-) diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs index 955d7f8613f..61afc00a676 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs @@ -371,10 +371,7 @@ internal static class ValidationHelper /// public static void ValidateNoNullArgument(object obj, string argumentName) { - if (obj == null) - { - throw new ArgumentNullException(argumentName); - } + ArgumentNullException.ThrowIfNull(obj, argumentName); } /// diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs index 8db2c626d6d..730e4d1d84f 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs @@ -367,10 +367,7 @@ public static T FindVisualAncestorData(this DependencyObject obj) /// The specified value is a null reference. public static T FindVisualAncestor(this DependencyObject @object) where T : class { - if (@object == null) - { - throw new ArgumentNullException("object"); - } + ArgumentNullException.ThrowIfNull(@object, "object"); DependencyObject parent = VisualTreeHelper.GetParent(@object); diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs index d91acdedb91..1d139afbd59 100644 --- a/src/Microsoft.WSMan.Management/ConfigProvider.cs +++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs @@ -3415,10 +3415,7 @@ private PSObject GetItemValue(string path) { ArgumentException.ThrowIfNullOrEmpty(path ); - if (path.Length == 0) - { - throw new ArgumentNullException(path); - } + ArgumentOutOfRangeException.ThrowIfZero(path.Length); // if endswith '\', removes it. if (path.EndsWith(WSManStringLiterals.DefaultPathSeparator.ToString(), StringComparison.OrdinalIgnoreCase)) diff --git a/src/Microsoft.WSMan.Management/CurrentConfigurations.cs b/src/Microsoft.WSMan.Management/CurrentConfigurations.cs index 641f558d432..6632f5bb6c2 100644 --- a/src/Microsoft.WSMan.Management/CurrentConfigurations.cs +++ b/src/Microsoft.WSMan.Management/CurrentConfigurations.cs @@ -140,10 +140,7 @@ public void UpdateOneConfiguration(string pathToNodeFromRoot, string configurati { ArgumentNullException.ThrowIfNull(pathToNodeFromRoot); - if (string.IsNullOrEmpty(configurationName)) - { - throw new ArgumentNullException(nameof(configurationName)); - } + ArgumentException.ThrowIfNullOrEmpty(configurationName); ArgumentNullException.ThrowIfNull(configurationValue); diff --git a/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs b/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs index e52582fc985..18cefd68c43 100644 --- a/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs +++ b/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs @@ -19,10 +19,7 @@ internal void Initialize(PSCmdlet cmdlet, string className, string classVersion, { ArgumentNullException.ThrowIfNull(cmdlet); - if (string.IsNullOrEmpty(className)) - { - throw new ArgumentNullException(nameof(className)); - } + ArgumentException.ThrowIfNullOrEmpty(className); // possible and ok to have classVersion==string.Empty ArgumentNullException.ThrowIfNull(classVersion); diff --git a/src/System.Management.Automation/engine/CommandInfo.cs b/src/System.Management.Automation/engine/CommandInfo.cs index 7cd213a025c..6b90b1d1640 100644 --- a/src/System.Management.Automation/engine/CommandInfo.cs +++ b/src/System.Management.Automation/engine/CommandInfo.cs @@ -285,10 +285,7 @@ internal void SetCommandType(CommandTypes newType) /// internal void Rename(string newName) { - if (string.IsNullOrEmpty(newName)) - { - throw new ArgumentNullException(nameof(newName)); - } + ArgumentException.ThrowIfNullOrEmpty(newName); Name = newName; } diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 113ba8121b8..5eb3b9b9247 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -278,7 +278,7 @@ public string[] ParameterName set { - _parameterNames = value ?? throw new ArgumentNullException(nameof(value)); + _parameterNames = value ?? ArgumentNullException.ThrowIfNull(value); _parameterNameWildcards = SessionStateUtilities.CreateWildcardsFromStrings( _parameterNames, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase); diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 670fe4a69cf..e041b0d7006 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -3914,11 +3914,7 @@ internal static Assembly LoadAssemblyFromFile(string fileName) internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module) { - if (assembly == null) - { - ArgumentNullException e = new ArgumentNullException(nameof(assembly)); - throw e; - } + ArgumentNullException.ThrowIfNull(assembly); string assemblyPath = assembly.Location; PSSnapInHelpers.AnalyzePSSnapInAssembly( diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 93a3e80b74b..f37743921ec 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1740,26 +1740,19 @@ internal static class Requires { internal static void NotNull(object value, string paramName) { - if (value == null) - { - throw new ArgumentNullException(paramName); - } + ArgumentNullException.ThrowIfNull(value, paramName); } internal static void NotNullOrEmpty(string value, string paramName) { - if (string.IsNullOrEmpty(value)) - { - throw new ArgumentNullException(paramName); - } + ArgumentException.ThrowIfNullOrEmpty(value, paramName); } internal static void NotNullOrEmpty(ICollection value, string paramName) { - if (value == null || value.Count == 0) - { - throw new ArgumentNullException(paramName); - } + ArgumentNullException.ThrowIfNull(value, paramName); + + ArgumentOutOfRangeException.ThrowIfZero(value.Count, paramName); } internal static void Condition([DoesNotReturnIf(false)] bool precondition, string paramName) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 51d84f8f6ab..6d8d766d50a 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -8025,15 +8025,9 @@ internal static bool CreateJunction(string path, string target) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")] private static bool WinCreateJunction(string path, string target) { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException(nameof(path)); - } + ArgumentException.ThrowIfNullOrEmpty(path); - if (string.IsNullOrEmpty(target)) - { - throw new ArgumentNullException(nameof(target)); - } + ArgumentException.ThrowIfNullOrEmpty(target); using (SafeHandle handle = WinOpenReparsePoint(path, FileDesiredAccess.GenericWrite)) { From d89a304a5e7a69f528f46408301f46d44d129b97 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 16:42:40 +0100 Subject: [PATCH 06/27] changed 8 files --- .../CimRegisterCimIndication.cs | 6 ++---- .../host/msh/CommandLineParameterParser.cs | 5 +---- .../engine/ComInterop/Helpers.cs | 5 +---- .../engine/lang/scriptblock.cs | 5 +---- .../engine/parser/SafeValues.cs | 8 ++++---- .../engine/remoting/commands/JobRepository.cs | 10 ++-------- .../namespaces/TransactedRegistryKey.cs | 8 ++------ .../utils/BackgroundDispatcher.cs | 2 +- 8 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs index 24b9332bde5..c08fa0c44ea 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs @@ -149,10 +149,8 @@ public void RegisterCimIndication( uint operationTimeout) { DebugHelper.WriteLogEx("queryDialect = '{0}'; queryExpression = '{1}'", 0, queryDialect, queryExpression); - if (cimSession == null) - { - throw new ArgumentNullException(string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession))); - } + + ArgumentNullException.ThrowIfNull(cimSession, string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession))); this.TargetComputerName = cimSession.ComputerName; CimSessionProxy proxy = CreateSessionProxy(cimSession, operationTimeout); diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index c3e2e196cbb..9e555bd428b 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -833,10 +833,7 @@ internal void Parse(string[] args) for (int i = 0; i < args.Length; i++) { - if (args[i] is null) - { - throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs); - } + ArgumentNullException.ThrowIfNull(args[i], CommandLineParameterParserStrings.NullElementInArgs); } // Indicates that we've called this method on this instance, and that when it's done, the state variables diff --git a/src/System.Management.Automation/engine/ComInterop/Helpers.cs b/src/System.Management.Automation/engine/ComInterop/Helpers.cs index 513c3126476..814e93825a7 100644 --- a/src/System.Management.Automation/engine/ComInterop/Helpers.cs +++ b/src/System.Management.Automation/engine/ComInterop/Helpers.cs @@ -35,10 +35,7 @@ internal static class Requires [System.Diagnostics.Conditional("DEBUG")] internal static void NotNull(object value, string paramName) { - if (value == null) - { - throw new ArgumentNullException(paramName); - } + ArgumentNullException.ThrowIfNull(value, paramName); } [System.Diagnostics.Conditional("DEBUG")] diff --git a/src/System.Management.Automation/engine/lang/scriptblock.cs b/src/System.Management.Automation/engine/lang/scriptblock.cs index 738c411bffd..cc2f8ef9138 100644 --- a/src/System.Management.Automation/engine/lang/scriptblock.cs +++ b/src/System.Management.Automation/engine/lang/scriptblock.cs @@ -1140,10 +1140,7 @@ public void Begin(InternalCommand command) { ArgumentNullException.ThrowIfNull(command); - if (command.MyInvocation == null) - { - throw new ArgumentNullException(nameof(command)); - } + ArgumentNullException.ThrowIfNull(command.MyInvocation, nameof(command)); Begin(command.MyInvocation.ExpectingInput, command.commandRuntime); } diff --git a/src/System.Management.Automation/engine/parser/SafeValues.cs b/src/System.Management.Automation/engine/parser/SafeValues.cs index 0e41f87a318..f551270c372 100644 --- a/src/System.Management.Automation/engine/parser/SafeValues.cs +++ b/src/System.Management.Automation/engine/parser/SafeValues.cs @@ -530,10 +530,10 @@ public object VisitIndexExpression(IndexExpressionAst indexExpressionAst) // Get the value of the index and value and call the compiler var index = indexExpressionAst.Index.Accept(this); var target = indexExpressionAst.Target.Accept(this); - if (index == null || target == null) - { - throw new ArgumentNullException(nameof(indexExpressionAst)); - } + + ArgumentNullException.ThrowIfNull(index, nameof(indexExpressionAst)); + + ArgumentNullException.ThrowIfNull(target, nameof(indexExpressionAst)); return GetIndexedValueFromTarget(target, index); } diff --git a/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs b/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs index 7bb52abcb1e..29d107a074f 100644 --- a/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs +++ b/src/System.Management.Automation/engine/remoting/commands/JobRepository.cs @@ -19,10 +19,7 @@ public abstract class Repository where T : class /// Object to add. public void Add(T item) { - if (item == null) - { - throw new ArgumentNullException(_identifier); - } + ArgumentNullException.ThrowIfNull(item, _identifier); lock (_syncObject) { @@ -45,10 +42,7 @@ public void Add(T item) /// Object to remove. public void Remove(T item) { - if (item == null) - { - throw new ArgumentNullException(_identifier); - } + ArgumentNullException.ThrowIfNull(item, _identifier); lock (_syncObject) { diff --git a/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs b/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs index 10826d25b21..0fa4795e81a 100644 --- a/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs +++ b/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs @@ -1388,8 +1388,7 @@ public void SetValue(string name, object value) [ComVisible(false)] public unsafe void SetValue(string name, object value, RegistryValueKind valueKind) { - if (value == null) - throw new ArgumentNullException(RegistryProviderStrings.Arg_Value); + ArgumentNullException.ThrowIfNull(value, RegistryProviderStrings.Arg_Value); if (name != null && name.Length > MaxValueNameLength) { @@ -2031,10 +2030,7 @@ private RegistryKeyPermissionCheck GetSubKeyPermissionCheck(bool subkeyWritable) private static void ValidateKeyName(string name) { - if (name == null) - { - throw new ArgumentNullException(RegistryProviderStrings.Arg_Name); - } + ArgumentException.ThrowIfNullOrEmpty(name, RegistryProviderStrings.Arg_Name); int nextSlash = name.IndexOf('\\'); int current = 0; diff --git a/src/System.Management.Automation/utils/BackgroundDispatcher.cs b/src/System.Management.Automation/utils/BackgroundDispatcher.cs index 8ef105ce0ea..0fa193cd834 100644 --- a/src/System.Management.Automation/utils/BackgroundDispatcher.cs +++ b/src/System.Management.Automation/utils/BackgroundDispatcher.cs @@ -70,7 +70,7 @@ public BackgroundDispatcher(EventProvider transferProvider, EventDescriptor tran // internal for unit testing only. Otherwise, would be private. internal BackgroundDispatcher(IMethodInvoker etwActivityMethodInvoker) { - _etwActivityMethodInvoker = etwActivityMethodInvoker ?? throw new ArgumentNullException(nameof(etwActivityMethodInvoker)); + _etwActivityMethodInvoker = etwActivityMethodInvoker ?? ArgumentNullException.ThrowIfNull(etwActivityMethodInvoker); _invokerWaitCallback = DoInvoker; } From 243cdf62d64852d1b1a50fc30ff3066c18553f4c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 17:15:40 +0100 Subject: [PATCH 07/27] fix GetCommandCommand.cs --- src/System.Management.Automation/engine/GetCommandCommand.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 5eb3b9b9247..94c018e1c87 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -278,7 +278,9 @@ public string[] ParameterName set { - _parameterNames = value ?? ArgumentNullException.ThrowIfNull(value); + ArgumentNullException.ThrowIfNull(value); + + _parameterNames = value ; _parameterNameWildcards = SessionStateUtilities.CreateWildcardsFromStrings( _parameterNames, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase); From d49a69958ae4c12707c66f29e334fbc7f062a3be Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 17:20:06 +0100 Subject: [PATCH 08/27] removed space --- src/System.Management.Automation/engine/GetCommandCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 94c018e1c87..81675d7b663 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -280,7 +280,7 @@ public string[] ParameterName { ArgumentNullException.ThrowIfNull(value); - _parameterNames = value ; + _parameterNames = value; _parameterNameWildcards = SessionStateUtilities.CreateWildcardsFromStrings( _parameterNames, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase); From ae3795d71502f4063a6e31a14fa5fca93dbb9d89 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 17:22:42 +0100 Subject: [PATCH 09/27] Update src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs Co-authored-by: Ilya --- .../ManagementList/Common/WpfHelp.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs index 730e4d1d84f..1795444e785 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs @@ -214,7 +214,7 @@ public static void AddChild(FrameworkElement parent, FrameworkElement element) { ArgumentNullException.ThrowIfNull(element); - ArgumentNullException.ThrowIfNull(parent, "element"); + ArgumentNullException.ThrowIfNull(parent, nameof(element)); ContentControl parentContentControl = parent as ContentControl; From 6466bfb273a8eac62c87f394f2f19eb2c4354d06 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 17:22:59 +0100 Subject: [PATCH 10/27] Update src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs Co-authored-by: Ilya --- .../ManagementList/Common/WpfHelp.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs index 1795444e785..42b8a06b845 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs @@ -367,7 +367,7 @@ public static T FindVisualAncestorData(this DependencyObject obj) /// The specified value is a null reference. public static T FindVisualAncestor(this DependencyObject @object) where T : class { - ArgumentNullException.ThrowIfNull(@object, "object"); + ArgumentNullException.ThrowIfNull(@object, nameof(@object)); DependencyObject parent = VisualTreeHelper.GetParent(@object); From fd5fe0ebaf38a1b2ba21383d8644a39b28f786cf Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 17:49:43 +0100 Subject: [PATCH 11/27] remove extra spaces --- .../ShowCommand/ViewModel/AllModulesViewModel.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs index 97884db7909..d2899b994d6 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs @@ -85,7 +85,7 @@ public AllModulesViewModel(Dictionary importedMod { throw new ArgumentNullException("commands"); } - + this.Initialization(importedModules, commands, true); } From 899b32284063cdec424c9038cef7818ccd804ef8 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 18:19:01 +0100 Subject: [PATCH 12/27] comment out - ArgumentOutOfRangeException.ThrowIfZero() --- src/Microsoft.WSMan.Management/ConfigProvider.cs | 10 +++++++--- src/System.Management.Automation/engine/Utils.cs | 6 +++++- .../utils/ParserException.cs | 8 ++++++-- .../utils/perfCounters/CounterSetRegistrarBase.cs | 8 ++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs index 1d139afbd59..b662a8f806c 100644 --- a/src/Microsoft.WSMan.Management/ConfigProvider.cs +++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs @@ -3413,9 +3413,13 @@ private static string NormalizePath(string path, string host) /// private PSObject GetItemValue(string path) { - ArgumentException.ThrowIfNullOrEmpty(path ); - - ArgumentOutOfRangeException.ThrowIfZero(path.Length); + ArgumentException.ThrowIfNullOrEmpty(path); + + // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(path.Length); + (path.Length == 0) + { + throw new ArgumentNullException(path); + } // if endswith '\', removes it. if (path.EndsWith(WSManStringLiterals.DefaultPathSeparator.ToString(), StringComparison.OrdinalIgnoreCase)) diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index f37743921ec..84588687f7e 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1752,7 +1752,11 @@ internal static void NotNullOrEmpty(ICollection value, string paramName) { ArgumentNullException.ThrowIfNull(value, paramName); - ArgumentOutOfRangeException.ThrowIfZero(value.Count, paramName); + // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(value.Count, paramName); + if (value.Count == 0) + { + throw new ArgumentNullException(paramName); + } } internal static void Condition([DoesNotReturnIf(false)] bool precondition, string paramName) diff --git a/src/System.Management.Automation/utils/ParserException.cs b/src/System.Management.Automation/utils/ParserException.cs index 0fcda917c3b..d4fd69a10cb 100644 --- a/src/System.Management.Automation/utils/ParserException.cs +++ b/src/System.Management.Automation/utils/ParserException.cs @@ -132,8 +132,12 @@ public ParseException(string message, public ParseException(Language.ParseError[] errors) { ArgumentNullException.ThrowIfNull(errors); - - ArgumentOutOfRangeException.ThrowIfZero(errors.Length); + + // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(errors.Length); + if (errors.Length == 0) + { + throw new ArgumentNullException(nameof(errors)); + } _errors = errors; // Arbitrarily choose the first error message for the ErrorId. diff --git a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs index fc6361b0351..0698790b06d 100644 --- a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs +++ b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs @@ -110,8 +110,12 @@ protected CounterSetRegistrarBase( ArgumentNullException.ThrowIfNull(counterInfoArray); - ArgumentOutOfRangeException.ThrowIfZero(counterInfoArray.Length); - + // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(counterInfoArray.Length); + if (counterInfoArray.Length == 0) + { + throw new ArgumentNullException(nameof(counterInfoArray)); + } + CounterInfoArray = new CounterInfo[counterInfoArray.Length]; for (int i = 0; i < counterInfoArray.Length; i++) From 166b05ffd08519796aa94c8fadb6eb935dae3f5a Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 18:21:56 +0100 Subject: [PATCH 13/27] ArgumentOutOfRangeException --- .../ManagementList/ManagementList/DefaultStringConverter.cs | 2 +- .../ShowCommand/ViewModel/AllModulesViewModel.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs index 2d590904097..c84aa34f94c 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs @@ -66,7 +66,7 @@ public object Convert(object[] values, Type targetType, object parameter, System if (values.Length != 1) { - throw new ArgumentNullException("values"); + throw new ArgumentOutOfRangeException("values"); } string val = values[0] as string; diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs index d2899b994d6..2bc9a01d028 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs @@ -83,7 +83,7 @@ public AllModulesViewModel(Dictionary importedMod if (!commands.GetEnumerator().MoveNext()) { - throw new ArgumentNullException("commands"); + throw new ArgumentOutOfRangeException("commands"); } this.Initialization(importedModules, commands, true); From 61d099d87d21826a9dce0f36827c078915481b3d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 18:33:28 +0100 Subject: [PATCH 14/27] revert ?? --- .../cimSupport/cmdletization/SessionBasedWrapper.cs | 2 +- .../commands/management/TestConnectionCommand.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs index 037fed9f92a..58a1fbb4c2d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs @@ -82,7 +82,7 @@ protected TSession[] Session set { - _session = value ?? ArgumentNullException.ThrowIfNull(value); + _session = value ?? throw new ArgumentNullException(nameof(value)); _sessionWasSpecified = true; } } diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index efc0c6dbec4..e6b33142457 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -578,7 +578,7 @@ private void ProcessMTUSize(string targetNameOrAddress) WriteObject(new PingMtuStatus( Source, resolvedTargetName, - replyResult ?? ArgumentNullException.ThrowIfNull(replyResult), + replyResult ?? throw new ArgumentNullException(nameof(replyResult)), CurrentMTUSize)); } } From 1d2b15cab33d67b1ae5a229352ee167128e15954 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 18:42:33 +0100 Subject: [PATCH 15/27] forgot if --- src/Microsoft.WSMan.Management/ConfigProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs index b662a8f806c..cccae26c821 100644 --- a/src/Microsoft.WSMan.Management/ConfigProvider.cs +++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs @@ -3416,7 +3416,7 @@ private PSObject GetItemValue(string path) ArgumentException.ThrowIfNullOrEmpty(path); // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(path.Length); - (path.Length == 0) + if (path.Length == 0) { throw new ArgumentNullException(path); } From 8252f13c752df22358196d542cc89cd6ee151d17 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 18:55:10 +0100 Subject: [PATCH 16/27] replace throw new system.ArgumentNullException --- .../engine/DefaultCommandRuntime.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs index 48d74667dae..88a2a2ee427 100644 --- a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs +++ b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs @@ -21,8 +21,7 @@ internal class DefaultCommandRuntime : ICommandRuntime2 /// public DefaultCommandRuntime(List outputList) { - if (outputList == null) - throw new System.ArgumentNullException(nameof(outputList)); + ArgumentNullException.ThrowIfNull(outputList); _output = outputList; } From b834d37c25542282080c6b1f1f94a9f2cbad81e3 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 19:10:32 +0100 Subject: [PATCH 17/27] Update src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs Co-authored-by: Ilya --- .../cimSupport/cmdletization/SessionBasedWrapper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs index 58a1fbb4c2d..8da4d7f4c78 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs @@ -82,7 +82,8 @@ protected TSession[] Session set { - _session = value ?? throw new ArgumentNullException(nameof(value)); + ArgumentNullException.ThrowIfNull(value); + _session = value; _sessionWasSpecified = true; } } From febe97286f852e17ced6ffd89b5b27aee1fc041c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 14 Dec 2022 19:43:51 +0100 Subject: [PATCH 18/27] fix test_FileSystemProvider.cs:line 87 --- test/xUnit/csharp/test_FileSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/xUnit/csharp/test_FileSystemProvider.cs b/test/xUnit/csharp/test_FileSystemProvider.cs index 85ab0b2eedd..1bea0f84ce4 100644 --- a/test/xUnit/csharp/test_FileSystemProvider.cs +++ b/test/xUnit/csharp/test_FileSystemProvider.cs @@ -84,7 +84,7 @@ public void TestCreateJunctionFails() } else { - Assert.Throws(delegate { InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty); }); + Assert.Throws(delegate { InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty); }); } } From 33e8fc2226a31f05be3efee90a9616cc97e11405 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 15 Dec 2022 00:30:25 +0100 Subject: [PATCH 19/27] Update with suggestion --- .../commands/management/TestConnectionCommand.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e6b33142457..e11be68156a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -575,10 +575,12 @@ private void ProcessMTUSize(string targetNameOrAddress) } else { + ArgumentNullException.ThrowIfNull(replyResult); + WriteObject(new PingMtuStatus( Source, resolvedTargetName, - replyResult ?? throw new ArgumentNullException(nameof(replyResult)), + replyResult, CurrentMTUSize)); } } From 5904d7e0b16405c7f2ab01ced24f2169b4867889 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 15 Dec 2022 11:11:21 +0100 Subject: [PATCH 20/27] revert --- .../ManagementList/Common/WpfHelp.cs | 5 +++- .../PropertyValueSelectorFilterRule.cs | 10 +++++-- .../FilterCore/ValidatingValueBase.cs | 5 +++- .../FilterProviders/FilterRulePanelItem.cs | 5 +++- .../cimSupport/cmdletization/cim/cimQuery.cs | 5 +++- .../ConfigProvider.cs | 5 +++- .../CurrentConfigurations.cs | 15 ++++++++--- src/Microsoft.WSMan.Management/WsManHelper.cs | 5 +++- .../CoreCLR/CorePsAssemblyLoadContext.cs | 5 +++- .../cmdletization/ObjectModelWrapper.cs | 5 +++- .../engine/CommandInfo.cs | 5 +++- .../engine/Modules/ModuleSpecification.cs | 5 +++- .../engine/Modules/PSModuleInfo.cs | 5 +++- .../engine/PSClassInfo.cs | 5 +++- .../engine/Utils.cs | 5 +++- .../remoting/commands/InvokeCommandCommand.cs | 5 +++- .../namespaces/TransactedRegistryKey.cs | 2 +- .../utils/MshTraceSource.cs | 26 ++++++++++++++----- .../utils/StructuredTraceSource.cs | 11 +++++--- 19 files changed, 103 insertions(+), 31 deletions(-) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs index 42b8a06b845..4bc9ebef28d 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs @@ -419,7 +419,10 @@ public static T GetOptionalTemplateChild(Control templateParent, string child { ArgumentNullException.ThrowIfNull(templateParent); - ArgumentException.ThrowIfNullOrEmpty(childName); + if (string.IsNullOrEmpty(childName)) + { + throw new ArgumentNullException("childName"); + } object templatePart = templateParent.Template.FindName(childName, templateParent); T item = templatePart as T; diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/FilterRules/PropertyValueSelectorFilterRule.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/FilterRules/PropertyValueSelectorFilterRule.cs index 2405273c6a7..f1bf8520a99 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/FilterRules/PropertyValueSelectorFilterRule.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/FilterRules/PropertyValueSelectorFilterRule.cs @@ -66,9 +66,15 @@ public PropertyValueSelectorFilterRule(string propertyName, string propertyDispl /// public PropertyValueSelectorFilterRule(string propertyName, string propertyDisplayName, IEnumerable rules) { - ArgumentException.ThrowIfNullOrEmpty(propertyName); + if (string.IsNullOrEmpty(propertyName)) + { + throw new ArgumentNullException("propertyName"); + } - ArgumentException.ThrowIfNullOrEmpty(propertyDisplayName); + if (string.IsNullOrEmpty(propertyDisplayName)) + { + throw new ArgumentNullException("propertyDisplayName"); + } ArgumentNullException.ThrowIfNull(rules); diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs index ab63bce9f27..f33862846a9 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs @@ -83,7 +83,10 @@ public string this[string columnName] { get { - ArgumentException.ThrowIfNullOrEmpty(columnName); + if (string.IsNullOrEmpty(columnName)) + { + throw new ArgumentNullException("columnName"); + } this.UpdateValidationResult(columnName); return this.GetValidationResult().ErrorMessage; diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRulePanelItem.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRulePanelItem.cs index 0887087f505..2378f9238d8 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRulePanelItem.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRulePanelItem.cs @@ -81,7 +81,10 @@ public FilterRulePanelItem(FilterRule rule, string groupId) { ArgumentNullException.ThrowIfNull(rule); - ArgumentException.ThrowIfNullOrEmpty(groupId); + if (string.IsNullOrEmpty(groupId)) + { + throw new ArgumentNullException("groupId"); + } this.Rule = rule; this.GroupId = groupId; diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimQuery.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimQuery.cs index 4c2a6521772..2c5c91fb1c8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimQuery.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimQuery.cs @@ -314,7 +314,10 @@ public override void FilterByAssociatedInstance(object associatedInstance, strin /// public override void AddQueryOption(string optionName, object optionValue) { - ArgumentException.ThrowIfNullOrEmpty(optionName); + if (string.IsNullOrEmpty(optionName)) + { + throw new ArgumentNullException(nameof(optionName)); + } ArgumentNullException.ThrowIfNull(optionValue); diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs index cccae26c821..28d5f78903d 100644 --- a/src/Microsoft.WSMan.Management/ConfigProvider.cs +++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs @@ -3413,7 +3413,10 @@ private static string NormalizePath(string path, string host) /// private PSObject GetItemValue(string path) { - ArgumentException.ThrowIfNullOrEmpty(path); + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentNullException(path); + } // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(path.Length); if (path.Length == 0) diff --git a/src/Microsoft.WSMan.Management/CurrentConfigurations.cs b/src/Microsoft.WSMan.Management/CurrentConfigurations.cs index 6632f5bb6c2..81d4ecfd870 100644 --- a/src/Microsoft.WSMan.Management/CurrentConfigurations.cs +++ b/src/Microsoft.WSMan.Management/CurrentConfigurations.cs @@ -76,7 +76,10 @@ public CurrentConfigurations(IWSManSession serverSession) /// False, if operation failed. public bool RefreshCurrentConfiguration(string responseOfGet) { - ArgumentException.ThrowIfNullOrEmpty(responseOfGet); + if (string.IsNullOrEmpty(responseOfGet)) + { + throw new ArgumentNullException(nameof(responseOfGet)); + } this.rootDocument.LoadXml(responseOfGet); this.documentElement = this.rootDocument.DocumentElement; @@ -95,7 +98,10 @@ public bool RefreshCurrentConfiguration(string responseOfGet) /// False, if operation is not successful. public void PutConfigurationOnServer(string resourceUri) { - ArgumentException.ThrowIfNullOrEmpty(resourceUri); + if (string.IsNullOrEmpty(resourceUri)) + { + throw new ArgumentNullException(nameof(resourceUri)); + } this.serverSession.Put(resourceUri, this.rootDocument.InnerXml, 0); } @@ -140,7 +146,10 @@ public void UpdateOneConfiguration(string pathToNodeFromRoot, string configurati { ArgumentNullException.ThrowIfNull(pathToNodeFromRoot); - ArgumentException.ThrowIfNullOrEmpty(configurationName); + if (string.IsNullOrEmpty(configurationName)) + { + throw new ArgumentNullException(nameof(configurationName)); + } ArgumentNullException.ThrowIfNull(configurationValue); diff --git a/src/Microsoft.WSMan.Management/WsManHelper.cs b/src/Microsoft.WSMan.Management/WsManHelper.cs index 0c3dfebe8f3..71672f3232f 100644 --- a/src/Microsoft.WSMan.Management/WsManHelper.cs +++ b/src/Microsoft.WSMan.Management/WsManHelper.cs @@ -180,7 +180,10 @@ private static string FormatResourceMsgFromResourcetextS( { ArgumentNullException.ThrowIfNull(resourceManager); - ArgumentException.ThrowIfNullOrEmpty(resourceName); + if (string.IsNullOrEmpty(resourceName)) + { + throw new ArgumentNullException(nameof(resourceName)); + } string template = resourceManager.GetString(resourceName); diff --git a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs index 93b9dc2d6e0..5cd38cec25b 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs @@ -579,7 +579,10 @@ public static class PowerShellAssemblyLoadContextInitializer /// public static void SetPowerShellAssemblyLoadContext([MarshalAs(UnmanagedType.LPWStr)] string basePaths) { - ArgumentException.ThrowIfNullOrEmpty(basePaths); + if (string.IsNullOrEmpty(basePaths)) + { + throw new ArgumentNullException(nameof(basePaths)); + } PowerShellAssemblyLoadContext.InitializeSingleton(basePaths); } diff --git a/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs b/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs index 18cefd68c43..e52582fc985 100644 --- a/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs +++ b/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs @@ -19,7 +19,10 @@ internal void Initialize(PSCmdlet cmdlet, string className, string classVersion, { ArgumentNullException.ThrowIfNull(cmdlet); - ArgumentException.ThrowIfNullOrEmpty(className); + if (string.IsNullOrEmpty(className)) + { + throw new ArgumentNullException(nameof(className)); + } // possible and ok to have classVersion==string.Empty ArgumentNullException.ThrowIfNull(classVersion); diff --git a/src/System.Management.Automation/engine/CommandInfo.cs b/src/System.Management.Automation/engine/CommandInfo.cs index 6b90b1d1640..7cd213a025c 100644 --- a/src/System.Management.Automation/engine/CommandInfo.cs +++ b/src/System.Management.Automation/engine/CommandInfo.cs @@ -285,7 +285,10 @@ internal void SetCommandType(CommandTypes newType) /// internal void Rename(string newName) { - ArgumentException.ThrowIfNullOrEmpty(newName); + if (string.IsNullOrEmpty(newName)) + { + throw new ArgumentNullException(nameof(newName)); + } Name = newName; } diff --git a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs index cdabb8b7d1e..b539b191909 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs @@ -44,7 +44,10 @@ public ModuleSpecification() /// The module name. public ModuleSpecification(string moduleName) { - ArgumentException.ThrowIfNullOrEmpty(moduleName); + if (string.IsNullOrEmpty(moduleName)) + { + throw new ArgumentNullException(nameof(moduleName)); + } this.Name = moduleName; // Alias name of miniumVersion diff --git a/src/System.Management.Automation/engine/Modules/PSModuleInfo.cs b/src/System.Management.Automation/engine/Modules/PSModuleInfo.cs index 6dddb43a0dd..8dae7db0deb 100644 --- a/src/System.Management.Automation/engine/Modules/PSModuleInfo.cs +++ b/src/System.Management.Automation/engine/Modules/PSModuleInfo.cs @@ -1302,7 +1302,10 @@ public object Invoke(ScriptBlock sb, params object[] args) /// public PSVariable GetVariableFromCallersModule(string variableName) { - ArgumentException.ThrowIfNullOrEmpty(variableName); + if (string.IsNullOrEmpty(variableName)) + { + throw new ArgumentNullException(nameof(variableName)); + } var context = LocalPipeline.GetExecutionContextFromTLS(); SessionState callersSessionState = null; diff --git a/src/System.Management.Automation/engine/PSClassInfo.cs b/src/System.Management.Automation/engine/PSClassInfo.cs index 10b2cf1101d..c73e9250071 100644 --- a/src/System.Management.Automation/engine/PSClassInfo.cs +++ b/src/System.Management.Automation/engine/PSClassInfo.cs @@ -61,7 +61,10 @@ public sealed class PSClassMemberInfo /// internal PSClassMemberInfo(string name, string memberType, string defaultValue) { - ArgumentException.ThrowIfNullOrEmpty(name); + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException(nameof(name)); + } this.Name = name; this.TypeName = memberType; diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 84588687f7e..c539cbe5d3a 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1745,7 +1745,10 @@ internal static void NotNull(object value, string paramName) internal static void NotNullOrEmpty(string value, string paramName) { - ArgumentException.ThrowIfNullOrEmpty(value, paramName); + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentNullException(paramName); + } } internal static void NotNullOrEmpty(ICollection value, string paramName) diff --git a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs index d56af99f7c7..a2a2397cb7a 100644 --- a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs @@ -2167,7 +2167,10 @@ public void StartProgress( return; } - ArgumentException.ThrowIfNullOrEmpty(computerName); + if (string.IsNullOrEmpty(computerName)) + { + throw new ArgumentNullException(nameof(computerName)); + } lock (_syncObject) { diff --git a/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs b/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs index 0fa4795e81a..32fce575a1b 100644 --- a/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs +++ b/src/System.Management.Automation/namespaces/TransactedRegistryKey.cs @@ -2030,7 +2030,7 @@ private RegistryKeyPermissionCheck GetSubKeyPermissionCheck(bool subkeyWritable) private static void ValidateKeyName(string name) { - ArgumentException.ThrowIfNullOrEmpty(name, RegistryProviderStrings.Arg_Name); + ArgumentNullException.ThrowIfNull(name, RegistryProviderStrings.Arg_Name); int nextSlash = name.IndexOf('\\'); int current = 0; diff --git a/src/System.Management.Automation/utils/MshTraceSource.cs b/src/System.Management.Automation/utils/MshTraceSource.cs index 7a6029d84b9..25fcc7b940e 100644 --- a/src/System.Management.Automation/utils/MshTraceSource.cs +++ b/src/System.Management.Automation/utils/MshTraceSource.cs @@ -98,10 +98,13 @@ internal static PSTraceSource GetTracer( string description, bool traceHeaders) { - // 2005/04/13-JonN In theory this should be ArgumentException, - // but I don't want to deal with loading the string in this - // low-level code. - ArgumentException.ThrowIfNullOrEmpty(name); + if (string.IsNullOrEmpty(name)) + { + // 2005/04/13-JonN In theory this should be ArgumentException, + // but I don't want to deal with loading the string in this + // low-level code. + throw new ArgumentNullException(nameof(name)); + } lock (PSTraceSource.s_getTracerLock) { @@ -225,7 +228,10 @@ internal static PSTraceSource GetNewTraceSource( /// Exception instance ready to throw. internal static PSArgumentNullException NewArgumentNullException(string paramName) { - ArgumentException.ThrowIfNullOrEmpty(paramName); + if (string.IsNullOrEmpty(paramName)) + { + throw new ArgumentNullException(nameof(paramName)); + } string message = StringUtil.Format(AutomationExceptions.ArgumentNull, paramName); var e = new PSArgumentNullException(paramName, message); @@ -281,7 +287,10 @@ internal static PSArgumentNullException NewArgumentNullException( /// Exception instance ready to throw. internal static PSArgumentException NewArgumentException(string paramName) { - ArgumentException.ThrowIfNullOrEmpty(paramName); + if (string.IsNullOrEmpty(paramName)) + { + throw new ArgumentNullException(nameof(paramName)); + } string message = StringUtil.Format(AutomationExceptions.Argument, paramName); // Note that the message param comes first @@ -468,7 +477,10 @@ internal static PSNotImplementedException NewNotImplementedException() /// Exception instance ready to throw. internal static PSArgumentOutOfRangeException NewArgumentOutOfRangeException(string paramName, object actualValue) { - ArgumentException.ThrowIfNullOrEmpty(paramName); + if (string.IsNullOrEmpty(paramName)) + { + throw new ArgumentNullException(nameof(paramName)); + } string message = StringUtil.Format(AutomationExceptions.ArgumentOutOfRange, paramName); var e = new PSArgumentOutOfRangeException(paramName, actualValue, message); diff --git a/src/System.Management.Automation/utils/StructuredTraceSource.cs b/src/System.Management.Automation/utils/StructuredTraceSource.cs index c13e9e5fe3d..8d47ac61c9d 100644 --- a/src/System.Management.Automation/utils/StructuredTraceSource.cs +++ b/src/System.Management.Automation/utils/StructuredTraceSource.cs @@ -269,10 +269,13 @@ public partial class PSTraceSource /// internal PSTraceSource(string fullName, string name, string description, bool traceHeaders) { - // 2005/04/13-JonN In theory this should be ArgumentException, - // but I don't want to deal with loading the string in this - // low-level code. - ArgumentException.ThrowIfNullOrEmpty(fullName); + if (string.IsNullOrEmpty(fullName)) + { + // 2005/04/13-JonN In theory this should be ArgumentException, + // but I don't want to deal with loading the string in this + // low-level code. + throw new ArgumentNullException(nameof(fullName)); + } try { From 17dae3dc2cc37d55a014a0b8c5eef4b899922d89 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 15 Dec 2022 17:51:21 +0100 Subject: [PATCH 21/27] Update src/Microsoft.WSMan.Management/ConfigProvider.cs Co-authored-by: Ilya --- src/Microsoft.WSMan.Management/ConfigProvider.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs index 28d5f78903d..98c76e31c89 100644 --- a/src/Microsoft.WSMan.Management/ConfigProvider.cs +++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs @@ -3418,12 +3418,6 @@ private PSObject GetItemValue(string path) throw new ArgumentNullException(path); } - // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(path.Length); - if (path.Length == 0) - { - throw new ArgumentNullException(path); - } - // if endswith '\', removes it. if (path.EndsWith(WSManStringLiterals.DefaultPathSeparator.ToString(), StringComparison.OrdinalIgnoreCase)) { From ecd907a57287320b761482fb3836153c1af4a3a8 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 15 Dec 2022 18:01:15 +0100 Subject: [PATCH 22/27] test_FileSystemProvider.cs --- test/xUnit/csharp/test_FileSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/xUnit/csharp/test_FileSystemProvider.cs b/test/xUnit/csharp/test_FileSystemProvider.cs index 1bea0f84ce4..85ab0b2eedd 100644 --- a/test/xUnit/csharp/test_FileSystemProvider.cs +++ b/test/xUnit/csharp/test_FileSystemProvider.cs @@ -84,7 +84,7 @@ public void TestCreateJunctionFails() } else { - Assert.Throws(delegate { InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty); }); + Assert.Throws(delegate { InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty); }); } } From 010931261b01653c40aa462cb7e25f38c176c59b Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 15 Dec 2022 18:15:31 +0100 Subject: [PATCH 23/27] FileSystemProvider.cs --- .../namespaces/FileSystemProvider.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 49bc926d537..2054d59470c 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7874,15 +7874,17 @@ internal static bool CreateJunction(string path, string target) { #if UNIX return false; - } - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")] - private static bool WinCreateJunction(string path, string target) - { - ArgumentException.ThrowIfNullOrEmpty(path); - - ArgumentException.ThrowIfNullOrEmpty(target); +#else + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentNullException(nameof(path)); + } + if (string.IsNullOrEmpty(target)) + { + throw new ArgumentNullException(nameof(target)); + } + using (SafeHandle handle = WinOpenReparsePoint(path, FileAccess.Write)) { byte[] mountPointBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(target)); From 33470463a0079ee3176f28bd0b0885f14b8fe8e9 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 15 Dec 2022 23:01:41 +0500 Subject: [PATCH 24/27] Update src/System.Management.Automation/namespaces/FileSystemProvider.cs --- .../namespaces/FileSystemProvider.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 2054d59470c..0ffc79ff6c4 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7884,7 +7884,6 @@ internal static bool CreateJunction(string path, string target) { throw new ArgumentNullException(nameof(target)); } - using (SafeHandle handle = WinOpenReparsePoint(path, FileAccess.Write)) { byte[] mountPointBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(target)); From 60e2307b742b04d93588f95cc31a5e372e38aa8c Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 16 Dec 2022 08:45:39 +0500 Subject: [PATCH 25/27] Update src/System.Management.Automation/utils/BackgroundDispatcher.cs --- src/System.Management.Automation/utils/BackgroundDispatcher.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/utils/BackgroundDispatcher.cs b/src/System.Management.Automation/utils/BackgroundDispatcher.cs index 0fa193cd834..ac74a32d352 100644 --- a/src/System.Management.Automation/utils/BackgroundDispatcher.cs +++ b/src/System.Management.Automation/utils/BackgroundDispatcher.cs @@ -70,7 +70,8 @@ public BackgroundDispatcher(EventProvider transferProvider, EventDescriptor tran // internal for unit testing only. Otherwise, would be private. internal BackgroundDispatcher(IMethodInvoker etwActivityMethodInvoker) { - _etwActivityMethodInvoker = etwActivityMethodInvoker ?? ArgumentNullException.ThrowIfNull(etwActivityMethodInvoker); + ArgumentNullException.ThrowIfNull(etwActivityMethodInvoker); + _etwActivityMethodInvoker = etwActivityMethodInvoker; _invokerWaitCallback = DoInvoker; } From 2933e959ae6dff785e0564ef01fc7e0f293a2381 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 16 Dec 2022 08:46:03 +0500 Subject: [PATCH 26/27] Update src/System.Management.Automation/engine/Utils.cs --- src/System.Management.Automation/engine/Utils.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index c539cbe5d3a..b472393ff53 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1755,7 +1755,6 @@ internal static void NotNullOrEmpty(ICollection value, string paramName) { ArgumentNullException.ThrowIfNull(value, paramName); - // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(value.Count, paramName); if (value.Count == 0) { throw new ArgumentNullException(paramName); From 3a9d5b2b4f650188097f9a9015b12f191060bbd3 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 16 Dec 2022 09:12:08 +0500 Subject: [PATCH 27/27] Apply suggestions from code review --- .../ManagementList/ManagementList/DefaultStringConverter.cs | 2 +- .../ShowCommand/ViewModel/AllModulesViewModel.cs | 2 +- src/System.Management.Automation/utils/ParserException.cs | 1 - .../utils/perfCounters/CounterSetRegistrarBase.cs | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs index c84aa34f94c..2d590904097 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs @@ -66,7 +66,7 @@ public object Convert(object[] values, Type targetType, object parameter, System if (values.Length != 1) { - throw new ArgumentOutOfRangeException("values"); + throw new ArgumentNullException("values"); } string val = values[0] as string; diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs index 2bc9a01d028..d2899b994d6 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs @@ -83,7 +83,7 @@ public AllModulesViewModel(Dictionary importedMod if (!commands.GetEnumerator().MoveNext()) { - throw new ArgumentOutOfRangeException("commands"); + throw new ArgumentNullException("commands"); } this.Initialization(importedModules, commands, true); diff --git a/src/System.Management.Automation/utils/ParserException.cs b/src/System.Management.Automation/utils/ParserException.cs index d4fd69a10cb..b7056108218 100644 --- a/src/System.Management.Automation/utils/ParserException.cs +++ b/src/System.Management.Automation/utils/ParserException.cs @@ -133,7 +133,6 @@ public ParseException(Language.ParseError[] errors) { ArgumentNullException.ThrowIfNull(errors); - // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(errors.Length); if (errors.Length == 0) { throw new ArgumentNullException(nameof(errors)); diff --git a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs index 0698790b06d..33e03e67a47 100644 --- a/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs +++ b/src/System.Management.Automation/utils/perfCounters/CounterSetRegistrarBase.cs @@ -110,7 +110,6 @@ protected CounterSetRegistrarBase( ArgumentNullException.ThrowIfNull(counterInfoArray); - // in .NET8.0 -> ArgumentOutOfRangeException.ThrowIfZero(counterInfoArray.Length); if (counterInfoArray.Length == 0) { throw new ArgumentNullException(nameof(counterInfoArray));