From b48943ce31534b12736df9768efc670c76a09163 Mon Sep 17 00:00:00 2001
From: CarloToso <105941898+CarloToso@users.noreply.github.com>
Date: Fri, 16 Dec 2022 12:46:45 +0100
Subject: [PATCH] Replace ArgumentNullException(nameof()) ->
ArgumentNullException.ThrowIfNull() 2 (#18792)
---
.../CimRegisterCimIndication.cs | 6 ++----
.../Utils.cs | 5 +----
.../ManagementList/Common/WpfHelp.cs | 10 ++--------
.../ManagementList/DefaultStringConverter.cs | 4 +++-
.../ShowCommand/ViewModel/AllModulesViewModel.cs | 4 +++-
.../cimSupport/cmdletization/SessionBasedWrapper.cs | 3 ++-
.../commands/management/TestConnectionCommand.cs | 4 +++-
.../Common/InvokeRestMethodCommand.Common.cs | 2 +-
.../host/msh/CommandLineParameterParser.cs | 5 +----
.../Eventing/EventProviderTraceListener.cs | 3 +--
src/Microsoft.WSMan.Management/ConfigProvider.cs | 2 +-
.../CoreCLR/CorePsAssemblyLoadContext.cs | 2 ++
.../engine/ComInterop/Helpers.cs | 5 +----
.../engine/DefaultCommandRuntime.cs | 3 +--
.../engine/GetCommandCommand.cs | 4 +++-
.../engine/InitialSessionState.cs | 12 ++----------
.../engine/PSClassInfo.cs | 2 ++
src/System.Management.Automation/engine/Utils.cs | 9 ++++-----
.../engine/lang/scriptblock.cs | 7 +++----
.../engine/parser/SafeValues.cs | 8 ++++----
.../engine/remoting/commands/JobRepository.cs | 10 ++--------
.../namespaces/FileSystemProvider.cs | 1 -
.../namespaces/TransactedRegistryKey.cs | 8 ++------
.../utils/BackgroundDispatcher.cs | 3 ++-
.../utils/ParserException.cs | 4 +++-
.../utils/perfCounters/CounterSetRegistrarBase.cs | 6 ++++--
26 files changed, 55 insertions(+), 77 deletions(-)
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
index 24b9332bde..c08fa0c44e 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
@@ -149,10 +149,8 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
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.Management.Infrastructure.CimCmdlets/Utils.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs
index 955d7f8613..61afc00a67 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs
@@ -371,10 +371,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
///
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 b0839c6ccd..4bc9ebef28 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 @@ namespace Microsoft.Management.UI.Internal
{
ArgumentNullException.ThrowIfNull(element);
- if (parent == null)
- {
- throw new ArgumentNullException("element");
- }
+ ArgumentNullException.ThrowIfNull(parent, nameof(element));
ContentControl parentContentControl = parent as ContentControl;
@@ -370,10 +367,7 @@ namespace Microsoft.Management.UI.Internal
/// 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, nameof(@object));
DependencyObject parent = VisualTreeHelper.GetParent(@object);
diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs
index cf85d79ae3..2d59090409 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 @@ namespace Microsoft.Management.UI.Internal
///
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 da51550c08..d2899b994d 100644
--- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs
+++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs
@@ -79,7 +79,9 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal
/// 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");
}
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs
index 58a1fbb4c2..8da4d7f4c7 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 @@ namespace Microsoft.PowerShell.Cmdletization
set
{
- _session = value ?? throw new ArgumentNullException(nameof(value));
+ ArgumentNullException.ThrowIfNull(value);
+ _session = 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 e6b3314245..e11be68156 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 @@ namespace Microsoft.PowerShell.Commands
}
else
{
+ ArgumentNullException.ThrowIfNull(replyResult);
+
WriteObject(new PingMtuStatus(
Source,
resolvedTargetName,
- replyResult ?? throw new ArgumentNullException(nameof(replyResult)),
+ 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 50a88bb675..a6a2a6290b 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 @@ namespace Microsoft.PowerShell.Commands
private static RestReturnType CheckReturnType(HttpResponseMessage response)
{
- if (response == null) { throw new ArgumentNullException(nameof(response)); }
+ ArgumentNullException.ThrowIfNull(response);
RestReturnType rt = RestReturnType.Detect;
string contentType = ContentHelper.GetContentType(response);
diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
index c3e2e196cb..9e555bd428 100644
--- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
+++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
@@ -833,10 +833,7 @@ namespace Microsoft.PowerShell
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/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs
index 94eb340b21..1c82891b65 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 @@ namespace System.Diagnostics.Eventing
[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/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs
index de99812b01..98c76e31c8 100644
--- a/src/Microsoft.WSMan.Management/ConfigProvider.cs
+++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs
@@ -3413,7 +3413,7 @@ namespace Microsoft.WSMan.Management
///
private PSObject GetItemValue(string path)
{
- if (string.IsNullOrEmpty(path) || (path.Length == 0))
+ if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
diff --git a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs
index 2e4e7fe0f9..5cd38cec25 100644
--- a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs
+++ b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs
@@ -580,7 +580,9 @@ namespace System.Management.Automation
public static void SetPowerShellAssemblyLoadContext([MarshalAs(UnmanagedType.LPWStr)] string basePaths)
{
if (string.IsNullOrEmpty(basePaths))
+ {
throw new ArgumentNullException(nameof(basePaths));
+ }
PowerShellAssemblyLoadContext.InitializeSingleton(basePaths);
}
diff --git a/src/System.Management.Automation/engine/ComInterop/Helpers.cs b/src/System.Management.Automation/engine/ComInterop/Helpers.cs
index 513c312647..814e93825a 100644
--- a/src/System.Management.Automation/engine/ComInterop/Helpers.cs
+++ b/src/System.Management.Automation/engine/ComInterop/Helpers.cs
@@ -35,10 +35,7 @@ namespace System.Management.Automation.ComInterop
[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/DefaultCommandRuntime.cs b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs
index 48d74667da..88a2a2ee42 100644
--- a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs
+++ b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs
@@ -21,8 +21,7 @@ namespace System.Management.Automation
///
public DefaultCommandRuntime(List